I have been trying to solve Project Euler problem 11. I have rechecked my code multiple times but still, after debugging, my program is not providing any answers
The question states that we need to find the greatest product possible of 4 consecutive numbers: either vertically, horizontally or diagonally of the matrix given in the code below:
Here is my code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int matr[20][20] =
{
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}
};
int horProduct=0, verProduct=0, diagright=0, diagleft=0;
int maxProduct = 0;
// horizontal product
for (int i = 0; i < 20; i++)
{
for(int j = 0; j < 17; j++)
{
horProduct = matr[i][j]*matr[i][j+1]*matr[i][j+2]*matr[i][j+3];
}
if(horProduct > maxProduct)
{
maxProduct = horProduct;
}
}
//vertical product
for (int j = 0; j < 20; j++)
{
for(int i = 0; i < 17; i++)
{
verProduct = matr[i][j]*matr[i+1][j]*matr[i+2][j]*matr[i+3][j];
}
if(verProduct > maxProduct)
{
maxProduct = verProduct;
}
}
//diagonal right
for (int i = 0; i < 17; i++)
{
for(int j = 0; j < 17; j++)
{
diagright = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3];
}
if(diagright > maxProduct)
{
maxProduct = diagright;
}
}
//diagonal left
for (int i = 19; i > 3; i--)
{
for(int j = 19; j > 3; j--)
{
diagleft = matr[i][j]*matr[i-1][j-1]*matr[i-2][j-2]*matr[i-3][j-3];
}
if(diagleft > maxProduct)
{
maxProduct = diagleft;
}
}
printf("final largest product: %d\n", maxProduct);
return 0;
}
kindly let me know what is going wrong here. Why isn't my program able to print any output?
I did a rookie mistake.
The comparison with maxProduct should be inside the second loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int matr[20][20] =
{
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}
};
int horProduct=0, verProduct=0, diagright=0, diagleft=0;
int maxProduct = 0;
// horizontal product
for (int i = 0; i < 20; i++)
{
for(int j = 0; j < 17; j++)
{
horProduct = matr[i][j]*matr[i][j+1]*matr[i][j+2]*matr[i][j+3];
if(horProduct > maxProduct)
{
maxProduct = horProduct;
}
}
}
printf("hori: %d\n", maxProduct);
for (int j = 0; j < 20; j++)
{
for(int i = 0; i < 17; i++)
{
verProduct = matr[i][j]*matr[i+1][j]*matr[i+2][j]*matr[i+3][j];
if(verProduct > maxProduct)
{
maxProduct = verProduct;
}
}
}
printf("verti: %d\n", maxProduct);
//diagonal right
for (int i = 0; i < 17; i++)
{
for(int j = 0; j < 17; j++)
{
diagright = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3];
if(diagright > maxProduct)
{
maxProduct = diagright;
}
}
}
printf("diagright: %d\n", maxProduct);
//diagonal left
for (int i = 0; i < 20 ; i++)
{
for(int j = 0; j < 17; j++)
{
diagleft = matr[i][j]*matr[i+1][j+1]*matr[i+2][j+2]*matr[i+3][j+3];
if(diagleft > maxProduct)
{
maxProduct = diagleft;
}
}
}
printf("diagleft: %d\n", maxProduct);
return 0;
}
In Scala:
Is there a way to directly split a string that contains 72 numeric values separated by ; into a 2-dimensional array of 9 rows and 8 columns with those numeric values -in numeric data type-?
val input = List.tabulate(72)(_.toString).mkString(";")
input.split(";").map(_.toInt).grouped(9).toArray
transforms
0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
into
Array(
Array(0, 1, 2, 3, 4, 5, 6, 7, 8),
Array(9, 10, 11, 12, 13, 14, 15, 16, 17),
Array(18, 19, 20, 21, 22, 23, 24, 25, 26),
Array(27, 28, 29, 30, 31, 32, 33, 34, 35),
Array(36, 37, 38, 39, 40, 41, 42, 43, 44),
Array(45, 46, 47, 48, 49, 50, 51, 52, 53),
Array(54, 55, 56, 57, 58, 59, 60, 61, 62),
Array(63, 64, 65, 66, 67, 68, 69, 70, 71)
)
If you want to swap the dimensions of rows/columns, replace 9 by 8.
using Range and grouped functions
scala> val a = (0 to 71).map(_.toString).toArray.mkString(";")
a: String = 0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71
scala> a.split(";").map(_.toInt).sliding(9,9).toArray
res269: Array[Array[Int]] = Array(Array(0, 1, 2, 3, 4, 5, 6, 7, 8), Array(9, 10, 11, 12, 13, 14, 15, 16, 17), Array(18, 19, 20, 21, 22, 23, 24, 25, 26), Array(27, 28, 29, 30, 31, 32, 33, 34, 35), Array(36, 37, 38, 39, 40, 41, 42, 43, 44), Array(45, 46, 47, 48, 49, 50, 51, 52, 53), Array(54, 55, 56, 57, 58, 59, 60, 61, 62), Array(63, 64, 65, 66, 67, 68, 69, 70, 71))
scala>
I'm writing a function that finds two identical elements in an array and then prints the value of their index. I can't seem to get the right second index. What's my problem?
code:
the function I'm troubled with called 'couplesSearch' and it returns its
value to 'printCouples'.line 139
#include <stdio.h>
#include <stdlib.h>
#define NUM_GUESTS 200
#define FALSE 0
#define TRUE 1
#define KIDS_AGE 12
float averageAge(int ages[], int size);
int meals(int guestAges[], int size);
int search(int guestAges[], int size);
void print(int condition);
int coupleSearch(int guestAges[], int size);
void printCouples(int size, int condition2, int ageFriend);
int main(void)
{
int guestAges[NUM_GUESTS] = {42, 108, 95, 101, 90, 5, 79, 79, 83, 105, 66, 66, 2, 28, 2, 12, 116, 63, 28, 37,
112, 85, 63, 34, 53, 23, 22, 117, 39, 96, 48, 7, 12, 19, 70, 113, 108, 20, 116,
55, 24, 52, 3, 94, 34, 105, 22, 32, 54, 29, 108, 45, 23, 118, 118, 20, 84, 22,
50, 59, 77, 36, 111, 43, 49, 107, 41, 63, 65, 89, 87, 46, 51, 10, 11, 111, 7, 22,
34, 69, 70, 24, 85, 35, 37, 81, 47, 57, 12, 29, 25, 40, 27, 44, 18, 59, 39, 43,
10, 102, 34, 36, 80, 19, 25, 91, 100, 27, 114, 67, 102, 66, 45, 113, 31, 70, 18,
94, 58, 73, 107, 91, 42, 37, 36, 48, 16, 95, 72, 53, 111, 71, 22, 5, 47, 71, 28,
72, 8, 58, 98, 48, 34, 64, 66, 30, 50, 39, 102, 109, 63, 107, 27, 71, 94, 9,
61, 72, 43, 96, 11, 120, 25, 18, 69, 4, 116, 82, 3, 111, 92, 117, 15, 101, 37, 22,
109, 40, 109, 5, 2, 55, 54, 80, 19, 99, 61, 69, 8, 108, 9, 14, 49, 44, 48, 22,
31, 18, 14, 35};
int size = 0;
printf("average is: %.2f\n", averageAge(guestAges,size));
printf("Number of kids 12 and under: %d\n" , meals(guestAges, size));
print(search(guestAges,size));
coupleSearch(guestAges,size);
/*printf("Two guests with age 16 at index %d and index %d");
printf("Youngest guest age: ");
printf("Oldest guest age: ");*/
return 0;
}
/*
Function will return average age of party guests
input: age array, number of guests
output: average age
*/
float averageAge(int guestAges[], int size)
{
float sum = 0;
float avg = 0;
for(size = 0; size < NUM_GUESTS; ++size)
{
sum += guestAges[size];
}
avg = sum / NUM_GUESTS;
return avg;
}
int meals(int guestAges[], int size)
{
int kids = 0;
for (size = 0; size < NUM_GUESTS; size++)
{
if (guestAges[size] == KIDS_AGE || guestAges[size] < KIDS_AGE)
{
kids++;
}
}
return kids;
}
int search(int guestAges[], int size)
{
int condition = 0;
int age = 0;
printf("Enter age to search: ");
scanf("%d", &age);
while (size < NUM_GUESTS && age != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition = TRUE;
} else
{
condition = FALSE;
}
return condition;
}
void print(int condition)
{
if (condition == TRUE)
{
printf("Guest found!\n");
}
else if (condition == FALSE)
{
printf("No guest this age.\n");
}
}
int coupleSearch(int guestAges[], int size)
{
int condition2 = 0;
int ageFriend = 0;
printf("Enter age of guest looking for friend: ");
scanf("%d", &ageFriend);
while (size < NUM_GUESTS && ageFriend != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition2 = TRUE;
} else {
condition2 = FALSE;
}
printCouples(size,condition2,ageFriend);
return condition2;
}
void printCouples(int size, int condition2, int ageFriend)
{
if (condition2 == TRUE)
{
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size+1, size+2);
}
else if (condition2 == FALSE)
{
printf("No guest this age.\n");
}
}
Thanks.
Your test assumes that there are two entries in the age array. That is, the age of the guest looking for a friend is not unique and that the array has been sorted. The array that you pass in is not sorted by age. Thus, size+2 will not contain the same age as size+1.
First sort your age array and then search for the first and last entry of that age in the array.
If you do not want to sort the array, then you would have to test every entry in the array and keep track of matching indexes.
You also exit coupleSearch() as soon as you have found the first match. You need to search for the second match separately if you do not want to sort and search for first and last match.
Since you print size+1, then your printed answer is one based.
You only search for the first match. In other words - you never search for a couple.
Maybe you should try something like:
void printCouples(int size, int condition2, int ageFriend)
{
int j = size+1; // Start after the first match
int found = 0;
while (j < NUM_GUESTS )
{
if (ageFriend == guestAges[j])
{
found = 1;
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size, j);
}
}
if (!found)
{
printf("No guest this age.\n");
}
}
Notice that condition2 isn't used anymore.
I have saved my JSONs in a database and I want to get specific objects from every entry.
I'll show it:
[
{
"Messungen":[
{
"car":"no Car choosed",
"g":0.2,
"time":"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]",
"max":66,
"secondText":"0 - 200 km\/h",
"speed":"[63, 61, 63, 62, 63, 64, 65, 65, 66, 66, 66, 64, 63, 63]",
"tosecond":"\/",
"to100":"\/",
"way":209,
"avg":63
}
]
},
{
"Messungen":[
{
"car":"no Car choosed",
"g":0.2,
"time":"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]",
"max":70,
"secondText":"0 - 200 km\/h",
"speed":"[59, 61, 61, 61, 62, 60, 61, 62, 61, 62, 63, 63, 64, 64, 63, 63, 64, 65, 65, 65, 65, 65, 66, 66, 67, 69, 69, 67, 67, 67, 67, 67, 68, 68, 69, 70, 70, 69, 69, 69, 69, 69, 68, 66, 66]",
"tosecond":"\/",
"to100":"\/",
"way":808,
"avg":65
}
]
}
]
I want to get the way and the car from the Array Messungen as a List or Array.
Is that possible or should I make two new entries in the database with these two types?
Thanks :)
Databases are designed to be good at managing many, many rows. Use the power of the database. Create a table called "Messung" and make each observation (car, g, way, u.s.w) a column in the table.