I set up two arrays one of 50 units big, array1[50] which has 50 random integers from a range of 50-100 and another array which prompts the user to enter in 10 intgers from the same range of numbers. My problem is, how do i compare the two, im tyring to find the number of times the 10 user inputted numbers match the numbers stored in the array1[50] which holds the seedeed random numbers. Ive tried to do a for loop within a for loop, like this.
array2[10]
array[50]
int counter = 0;
for(int i = 0; i < 10; ++i){
for(int k = 0; k < 50; ++k){ //i've tried this and it does not work, i don't know what else to do
if( array2[i] == array[k]);
++counter;
}
}
//any help is appreciated thanks.
There are easier ways to achieve this using other libraries. But considering you're working with small arrays, you could use your code with the following modification:
array2[10]
array[50]
int counter = 0;
for(int i = 0; i < 10; ++i){
for(int k = 0; k < 50; ++k){
if( array2[i] == array[k])counter++;
}
}
That should work.
Related
I would like to know if there's a way for me to get the current value of "j" outside of the foor loop whenever the conditions is true. The variable "totalvalid" will tell me how many times the condition was met but I would also like to know the exact value of j when the condition is true so that I can use it at a later point. So I would want to extract the value of "j" whenever the "totalvalid = totalvalid +1" happens. Sorry if it looks messy. I'm new to coding and still have no idea how to make it cleaner. Thank you.
for(int j = 0; j < stringnumber; j++){
int valid = 0;
if(str[j][10] == '\0'){
for(int k = 0; k < 10; k++){
if(str[j][k] >= 'A' && str[j][k] <= 'Z'){
valid++;
}
}
if (valid == 10){
totalvalid = totalvalid + 1;
}
}
}
It seems that you want an array of numbers from that pass the condition.
My suggestion would be to make an array of ints, where you will keep these numbers.
Before loop:
int *array_of_valid_ints = (int *) calloc(stringnumber, sizeof(int)); // allocate the array
int number_of_valid_ints = 0;
Inside the if statement:
array_of_valid_ints[number_of_valid_ints] = j;
number_of_valid_ints++;
After the loop ends, you can check the good values with:
printf("This are the good ints: ")
for (int i = 0; i < number_of_valid_ints; i++) {
printf("%d ", array_of_valid_ints[i]);
}
printf("\n");
maybe you can define a variable before the loop as int j=0; then use a while loop instead of for.also remember to write j++ in the while loop.this way you can use the value of j outside of the loop too!
I need some assistance in C language.
Assuming I have an array of 10 elements:
int arr[10] = {1,2,999,4,5,999,7,999,9,10};
I want to add the number 1000 at every position where 999 is found without deleting it of course. Or in that case positions where 1000 has to be added: arr[2], arr[5], arr[7]
So my result buffer would be after compiling (of course increased by the amount of positions where 999 has been added):
temp[100] = {1,2,1000,999,4,5,1000,999,7,1000,999,9,10};
Can you help me with that?
You can do this by using conditionals like given below.
//I assume that the arrays are already declared
int i,j;
for(i = 0, j = 0; i < n; i++ , j++){ //here n is the size of the array
if(arr[j] == 999){
temp[i] = 1000;
i++; n++;
temp[i] = 999;
}
else
temp[i] = arr[j];
}
Try this out. This code snippet may not seem so standard but this gives you your desired output...
I have different paths that are predefined in a 2-D array as [seed][sequence] with 'seed' being the path (0-255) and 'sequence' being the index in the path (0-49). Is there a way to neatly compare an input-defined number of paths at once when the number of paths compared may vary from run to run?
How my input is gathered:
#define MAX 250
int numUsers; //the number of users
int users[MAX][2]; //each user and their respective seed
int collisions = 0; //number of collisions
static inline void compareNumUsers(){
printf("Enter the number of users: ");
scanf("%d", &numUsers);
printf("\n");
for(int i = 0; i < numUsers; i++){
printf("Enter the seed of user%d [0-255]: ", i+1);
scanf("%d", &users[i][1]);
}
}
Best case scenario the answer is not a series of case-switch statements
I am hoping that something to the effect of this will be possible:
(assume 3 given inputs)
for(int j = 0; j < pathLength; j++){
if(paths[users[0][1]][j] == paths[users[1][1]][j] || paths[users[1][1]][j] == paths[users[2][1]][j] || paths[users[2][1]][j] == paths[users[0][1]][j])
collisions++;
}
I would be lying if I said I did not confuse myself when thinking how to even ask this question so if you are confused by something please ask me to verify I will not be offended.
The comparison also does not have to be in the same manner that I provided, it is just how I presently think about it.
Any help is appreciated. Thank you in advance.
Use nested loops to compare all the pairs.
for (int j = 0; j < pathlength; j++) {
for (int i = 0, broken = 0; !broken && i < numUsers - 1; i++) {
for (int k = i + 1; k < numUsers; k++) {
if (paths[users[i][1]][j] == paths[users[k][1]][j]) {
collisions++;
broken = 1;
break;
}
}
}
}
I'm building in C language, a game called 4-in-a-row or Connect Four, for a fast review of the game you can see here:
http://en.wikipedia.org/wiki/Connect_Four
so, I have a 2 dimensional array of size [6][7], and I want to check in diagonal if there are 4 tokens which are "*" or "o" that are defined as a chars which are in a a row. I'm trying to write a function that after each play, it sums up all the possible diagonals and see if the sum is 4 for example, or if we want to check in pairs, if we get three similar pairs then there are 4 equal tokens in a row, so in this case the sum is 3, and so on..
for all I know, there are 12 different different diagonals (every 6 on different direction), how do u suggest me to write this function while being the most effective? and also including all the possibilities with less that 16 lines of code.
any kind of help would be appreciated!
here is an example of what I did:
int CheckDiagonal_1(char matrix[Rows][Columns])
{
int s_count = 0;
int o_count = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 5; j >= 3; j--)
{
for(int k = 0; k <= 3; k++)
{
if(matrix[j-k][i+k]== matrix[j-k-1][i+k+1]) count ++;
if(count==4) return count;
}
count = 0;
}
}
return 0;
}
Diagonals are sequences where
i == j + c for i from (0,height) and c (-width, height)
or i == -j + c.
So if goal to write code that fits into small number of lines - just write loops that go over i {0-6} and check for indexes to fit in range. Something like
for (int c= -7; c < 7; c++)
{
int starsOnDiag = 0;
for(int i = 0; i < 7; i++)
{
starsOnDiag += !indexesInRange(i, j) ? 0 :
cell[i, i+c] == '*' ? 1 : 0;
}
... // other diagonal and check for other symbol
}
for example
int count=0
for(int i=0;i<12;i++)
for(int j=i+1;j<10;j++)
for(int k=j+1;k<8;k++)
count++;
System.out.println("count = "+count);
or
for(int i=0;i<I;i++)
for(int j=i+1;j<J;j++)
for(int k=j+1;k<K;k++)
:
:
:
for(int z=y+1;z,<Z;z,++,)
count++;
what is value of count after all iteration? Is there any formula to calculate it?
It's a math problem of summation
Basically, one can prove that:
for (i=a; i<b; i++)
count+=1
is equivalent to
count+=b-a
Similarly,
for (i=a; i<b; i++)
count+=i
is equivalent to
count+= 0.5 * (b*(b+1) - a*(a+1))
You can get similar formulas using for instance wolframalpha (Wolfram's Mathematica)
This system will do the symbolic calculation for you, so for instance,
for(int i=0;i<A;i++)
for(int j=i+1;j<B;j++)
for(int k=j+1;k<C;k++)
count++
is a Mathematica query:
http://www.wolframalpha.com/input/?i=Sum[Sum[Sum[1,{k,j%2B1,C-1}],{j,i%2B1,B-1}],{i,0,A-1}]
Not a full answer but when i, j and k are all the same (say they're all n) the formula is C(n, nb_for_loops), which may already interest you :)
final int n = 50;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
for (int l = k+1; l < n; l++) {
count++;
}
}
}
}
System.out.println( count );
Will give 230300 which is C(50,4).
You can compute this easily using the binomail coefficient:
http://en.wikipedia.org/wiki/Binomial_coefficient
One formula to compute this is: n! / (k! * (n-k)!)
For example if you want to know how many different sets of 5 cards can be taken out of a 52 cards deck, you can either use 5 nested loops or use the formula above, they'll both give: 2 598 960
That's roughly the volume of an hyperpyramid http://www.physicsinsights.org/pyramids-1.html => 1/d * (n ^d) (with d dimension)
The formula works for real number so you have to adapt it for integer
(for the case d=2 (the hyperpyramid is a triangle then) , 1/2*(n*n) becomes the well know formula n(n+1)/2 (or n(n-1)/2) depending if you include the diagonal or not). I let you do the math
I think the fact your not using n all time but I,J,K is not a problem as you can rewrite each loop as 2 loop stopping in the middle so they all stop as the same number
the formula might becomes 1/d*((n/2)^d)*2 (I'm not sure, but something similar should be ok)
That's not really the answer to your question but I hope that will help to find a real one.