For loop within a for loop in c - c

For some reason my outer for loop does not seem to be doing anything, I have checked all the paranthesis and everything looks ok but it is still not looping.
With this program I want to sum 50 random numbers (the numbers can either be 1 or -1...it's for a computational physics problem) and print the magnitude, which the program does. BUT I want to go further and do this 10 times, and calculate the average magnitude.
I know what I need to do I'm just having a problem with this loop.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h> //Neeed this to seed the random generator with time, or else it will always generate the same numbers.
//This is a program to calculate the magnitude of the displacement of a particle after random collisions.
#define RAND_MAX 1
int main()
{
//using ints because the particle can only move one unit on x-axis at a time.
int i, x, displacement, sum = 0, avg;
int total_disp=0, mag_disp;
srand(time(NULL));
//Each collision causes the particle to advance or retreat by one unit on the x axis.
for(i=0; i<10; i++)
{
for (i=0; i<50; i++) //for 50 collisions
{
x = rand() % 2; //generates random numbers between 0 and 1.
if (x==0) //if x is 0 then it was a displacement in the minus x direction
{
displacement = -1;
}
else { //if x is 1 it is a displacement in the minus x direction
displacement = 1;
}
printf("the disp is : %d\n", displacement);
total_disp = total_disp + displacement; //sum the displacements
}
if (total_disp < 0) {
mag_disp = total_disp * -1;
}
else{ mag_disp = total_disp; }
printf("The total displacement is: %d\n", mag_disp);
sum = sum + mag_disp; //sum of the displacement magnitudes, there should be ten of them in this case
avg = sum / i; //average displacement is the sum of all the magnitudes divded by the number of times this is performed.
printf("The average displacement for i = %d particles is: %d", i, avg);
}
return 0;
}

You cannot use the same iteration variable in both loops.
for(i=0; i<10; i++)
for (i=0; i<50; i++)
increments i to 50 in the first iteration of the outer loop.
for(i=0; i<10; i++)
for (j=0; j<50; j++)
will work.

You're using the same variable i for both loops. You should use different variables.
...
for(j=0; j<10; j++)
{
for (i=0; i<50; i++) //for 50 collisions
{
...

Use different loop variables for the inner and outer loops.
When the first iteration of the inner loop is done i == 50, so the outer loop is also done.

Try this:
for(int i=0; i<10; i++)
{
for(int j=0; j<50; j++)
{ ... }
}
And change your variables accordingly.

Dennis is right.
in ANSI C, i and j must be declared in the begining of the function :
int main()
{
int i,j;
...//instructions
for( i=0; i<10; i++)
{
for( j=0; j<50; j++)
{ ... }
}

Related

How to write the algorithm of this matrix in this ascending order in C.Where is error in my code?

I am writing this code to print the following matrix in this spiral order(spiral by column).But my code is printing totally different thing.
a a+7 a+8 a+15
a+1 a+6 a+9 a+14
a+2 a+5 a+10 a+13
a+3 a+4 a+11 a+12
Here is what i did:
int main() {
int a;
int Sum = 0;
int i = 0, j = 0,n;
printf("Insert the value of n: ");
scanf("%d",&n);
printf("Insert the value of a number: ");
scanf("%d",&a);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",a);
a = a + 7;
printf("\t");
}
printf("%d",a);
a = a + 1 ;
printf("\n");
}
return 0;
}
The way I approached this is to build the matrix of values you actually want, but doing so in column order, where we can relatively easily control the logic of value progression by row. Then, with that matrix in hand, print out the values in row order, as you want the output:
int main()
{
int a = 7;
int n = 4;
int array[4][4];
for (int c=0; c < n; ++c)
{
for (int r=0; r < n; ++r)
{
// values ascending for even columns
if (c % 2 == 0)
{
array[r][c] = a + c*n + r;
}
// values descending for odd columns
else
{
array[r][c] = a + c*n + n-r-1;
}
}
}
for (int i=0; i < n; ++i)
{
for (int j=0; j < n; ++j)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}
Output:
Demo here:
Rextester
Instead of using this complex mechanism to keep track of all elements you can just calculate the value to add at any time by simple arithmetic.
See this
int row;
int column;
printf("\n");
for (row = 0; row < n; row++) {
for (column = 0; column < n; column++) {
int base;
int flag;
if (column % 2 != 0) {
base = (column+1)/2 * 2*n - 1;
flag = -1;
}else {
base = column/2 * 2*n;
flag = 1;
}
printf( "%d ", a + base + flag * row);
}
printf("\n");
}
I hope you are able to follow this logic. If not feel free to ask.
Demo here:
Ideone
There seem to be two issues with your code as it is. As mentioned in the above comment, you are using the variable a in the loop calculation, so it is constantly being updated. This means your loop becomes invalid after a few iterations. If you define a dummy variable, this would avoid the problem. Secondly the implementation of the spiralling is close to being right, but it's not quite there.
Consider in the case n = 4. When you print along each row, the difference between a new element and the last alternates between values of (2n - 1) = 7 and 1. To take this into account, you could for example check every time you want to print whether the column index (j) is odd or even, and use this to determine which difference to add. Once you have the row machinery fixed, it shouldn't be difficult to extend it to the columns.
Simple solution using a matrix to calculate values before print them
#include <stdio.h>
int main(void)
{
int a;
int i = 0, j = 0, n;
printf("Insert the value of n: ");
scanf("%d", &n);
printf("Insert the value of a number: ");
scanf("%d", &a);
int matrix[n][n];
for (i=0; i< n*n; i++)
{
// even columns ascending
if (((i/n) % 2) == 0)
{
matrix[i%n][i/n] = a++;
}
// odd column descending
else
{
matrix[n-(i%n)-1][i/n] = a++;
}
}
for (i=0; i< n; i++)
{
for (j=0; j< n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Insert the value of n: 4
Insert start value: 1
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13

Scramble numbers from array with numbers - C

I have been figuring out how to scramble numbers from array after user enters 10 different numbers by using rand(). It crushes when it arrives to adjust() function so feel free to point out my stupid mistake. Cheers. The top part is function, the bottom part is in main().
void adjust(int z[], int size)
{
int i, n, t;
for(i = 0; i < size; i++)
{
size = rand();
t = z[size];
z[size] = z[i];
z[i] = t;
}
printf("\nYour numbers have been scrambled and here they are: \n", t);
}
.....................
int z[10];
int i;
int num = 0;
printf("Please enter 10 different numbers: \n");
for(i = 0; i < 10; i++)
{
z[i] = num;
scanf("%d", &num);
}
printf("\nThe numbers you entered were: ");
for (i = num; i <= 10; i++)
{
printf("%d ", z[i]);
}
printf("\n");
addNum(z, 10);
adjust(z, 10);
return 0;
The rand() function returns a number between 0 and RAND_MAX.
Hence, the array index can go well beyond its range.
To get a random index within a range from 0 to N -1 , use rand() % N.
Another issue is that in your for loop, in adjust function, you are destroying the original value of 'size'. That contains the length of your array and is used to check the terminating condition of your for loop. Hence, do not modify 'size'. Use another variable to store your random index.
for(i = 0; i < size; i++)
{
n = rand() % size; // n is between 0 and size-1
t = z[n];
z[n] = z[i];
z[i] = t;
}
// For a better design move the following lines to a separate function
// that way adjust function just does the scrambling while another
// printing function prints out the array. Each function does only one thing.
printf("\nYour numbers have been scrambled and here they are: \n");
for( i = 0; i < size; i++)
{
printf("%d ", z[i]);
}

Was told my C program was "hard coded" and I don't understand why

I turned in my assignment to my online C programming class and was docked huge due to the fact that my program was "hard coded, and I can't see how it would be considered "hard coded" as I ask for user input. The following was my code:
#include <stdio.h>
#include <stdlib.h>
#define IMAX 3
#define JMAX 4
int main()
{
float a[IMAX][JMAX];
float avgrow[5];
float avgcol[5];
int i,j;
char c;
printf ("This program will allow you to enter numbers for 3 rows and 4 columns from left to right then filling down, and take the averages of the rows and columns and list them next to the row and under the columns. You may use decimals but only 2 will display in the results. Press enter!\n");
scanf ("%c",&c);
printf("Enter 12 numbers here for your rows and columns:\n");
for(i = 0; i < IMAX; i++)
{
for(j = 0; j < JMAX; j++)
{
scanf("%f",&a[i][j]);
}
}
for(j = 0; j < JMAX; j++)
{
avgrow[0] = (a[0][0]+a[0][1]+a[0][2]+a[0][3])/JMAX;
avgrow[1] = (a[1][0]+a[1][1]+a[1][2]+a[1][3])/JMAX;
avgrow[2] = (a[2][0]+a[2][1]+a[2][2]+a[2][3])/JMAX;
}
for(i=0; i < IMAX; i++)
{
avgcol[0] = (a[0][0]+a[1][0]+a[2][0])/IMAX;
avgcol[1] = (a[0][1]+a[1][1]+a[2][1])/IMAX;
avgcol[2] = (a[0][2]+a[1][2]+a[2][2])/IMAX;
avgcol[3] = (a[0][3]+a[1][3]+a[2][3])/IMAX;
}
printf(" Column1 Column2 Column3 Column4 Row Average\n\n");
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[0][0],a[0][1],a[0][2],a[0][3],avgrow[0]);
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[1][0],a[1][1],a[1][2],a[1][3],avgrow[1]);
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[2][0],a[2][1],a[2][2],a[2][3],avgrow[2]);
printf("\n");
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t\n",avgcol[0],avgcol[1],avgcol[2],avgcol[3]);
return 0;
}
All it was supposed to do was make a 2-d array with 3 rows and 4 columns, then take the average of the rows and display that next to to the row in a table. Than take the average of the columns and display them beneath the columns in the table.
This was his comments on my assignment "Well, you got the correct answers, but when dealing with a 2-D array, you should use nested for loops. Not one for loop and then a lot of "hard coding" values into the program."
Any help deciphering this would be appreciated as I though I was finally understanding programming until this.
First of all it's not meaningful to talk about a program being hard coded or not. Rather one would talk about specific values being hard coded. What this means is that you wrote their values directly into the code rather than putting them in a constant or variable that can easily be changed.
In this case the values you hard-coded are the number of rows and the number of columns. You do have constants for these, but you don't use them consistently. That is if you changed your constants to turn the array into a 5x5 array, your code would now break because parts of the code would still act like it is an 3x4 array.
Specifically there are two loops in your code where you're accessing the indices [0][0] through [2][3] by spelling out each index in that range specifically rather than using a loop. This means that if you change IMAX and JMAX, it will still use those same indices, which aren't correct any more.
So your array indices are hard-coded and changing the array dimensions breaks your program.
for(j = 0; j < JMAX; j++)
{
avgrow[0] = (a[0][0]+a[0][1]+a[0][2]+a[0][3])/JMAX;
avgrow[1] = (a[1][0]+a[1][1]+a[1][2]+a[1][3])/JMAX;
avgrow[2] = (a[2][0]+a[2][1]+a[2][2]+a[2][3])/JMAX;
}
for(i=0; i < IMAX; i++)
{
avgcol[0] = (a[0][0]+a[1][0]+a[2][0])/IMAX;
avgcol[1] = (a[0][1]+a[1][1]+a[2][1])/IMAX;
avgcol[2] = (a[0][2]+a[1][2]+a[2][2])/IMAX;
avgcol[3] = (a[0][3]+a[1][3]+a[2][3])/IMAX;
}
Notice the copying/pasting of nearly identical code? That's often a sign of hardcoding -- the presence of constants in the code's text or structure. How do you change the 3 and the 4? They're "hard" -- built into the code.
The proof it's a problem -- you have:
#define IMAX 3
#define JMAX 4
But if you actually change those, the code will break. Look at this line of code:
avgrow[0] = (a[0][0]+a[0][1]+a[0][2]+a[0][3])/JMAX;
That's an average if, and only if, JMAX is 4. The code was build with the understanding that JMAX had to be 4 -- JMAX was "hard coded" to 4.
Looking at the following code:
for(j = 0; j < JMAX; j++)
{
avgrow[0] = (a[0][0]+a[0][1]+a[0][2]+a[0][3])/JMAX;
avgrow[1] = (a[1][0]+a[1][1]+a[1][2]+a[1][3])/JMAX;
avgrow[2] = (a[2][0]+a[2][1]+a[2][2]+a[2][3])/JMAX;
}
This code assumes that a always has 3 rows and 4 columns, regardless of how a was actually declared. If you changed JMAX to 2, for example, then your code above would break because a would have dimension 3x2, and you'd be attempting to access elements outside of the array bounds.
What your instructor was looking for was something along these lines:
for(j = 0; j < JMAX; j++)
{
float sum = 0.0;
for (i = 0; i < IMAX; i++ )
{
sum += a[i][j];
}
avgrow[j] = sum/JMAX;
}
This code makes no assumptions about the dimensions of a beyond what is specified by IMAX and JMAX.
Note also that your declarations for avgrow and avgcol are hard-coded to 5, when they should also be based on IMAX and JMAX:
float avgrow[IMAX];
float avgcol[JMAX];
float avgrow[5];
float avgcol[5];
for(j = 0; j < JMAX; j++)
{
avgrow[0] = (a[0][0]+a[0][1]+a[0][2]+a[0][3])/JMAX;
avgrow[1] = (a[1][0]+a[1][1]+a[1][2]+a[1][3])/JMAX;
avgrow[2] = (a[2][0]+a[2][1]+a[2][2]+a[2][3])/JMAX;
}
for(i=0; i < IMAX; i++)
{
avgcol[0] = (a[0][0]+a[1][0]+a[2][0])/IMAX;
avgcol[1] = (a[0][1]+a[1][1]+a[2][1])/IMAX;
avgcol[2] = (a[0][2]+a[1][2]+a[2][2])/IMAX;
avgcol[3] = (a[0][3]+a[1][3]+a[2][3])/IMAX;
}
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[0][0],a[0][1],a[0][2],a[0][3],avgrow[0]);
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[1][0],a[1][1],a[1][2],a[1][3],avgrow[1]);
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\n",a[2][0],a[2][1],a[2][2],a[2][3],avgrow[2]);
printf("\n");
printf("%8.2f\t%8.2f\t%8.2f\t%8.2f\t\n",avgcol[0],avgcol[1],avgcol[2],avgcol[3]);
THESE ALL STEPS YOU HAVE TAKEN ARE HARD CODING AS YOU ARE SPECIFICALLY MENTIONING THE INDICES IF SUPOSE the value imax and jmax changes then you have to manually change/add avgcol[index] in taking averages as well as in output...
Your programm should be independent of it it should only depend on the value of i max and jamx
for a reference you can view a sample of your code in which i have removed hardcoding
http://ideone.com/mXymKS
Although this code could be simplied to a great extent...
You have used explicit integer values for the array indices of avgrow and avgcol. To avoid hard-coding, try using a loop with an integer variable as index, e.g.,
for(int k=0; k<4; ++k)
and then assign values to avgrow[k].
Your professor expected you to be able to modify the number of line and columns in your program easily. The disadvantage of the program you wrote is that a modification of one of those two parameters require you to change the whole program. You can achieve more flexibility for instance like this:
#include <stdio.h>
#include <stdlib.h>
#define IMAX 3
#define JMAX 4
int main()
{
float a[IMAX][JMAX];
float avgrow[IMAX] = {0};
float avgcol[JMAX] = {0};
printf ("This program will allow you to enter numbers for %d rows"
"and %d columns from left to right then filling down, and"
" take the averages of the rows and columns and list them"
" next to the row and under the columns. You may use "
"decimals but only 2 will display in the results. Press"
" enter!\n", IMAX, JMAX);
char c;
scanf ("%c",&c);
printf("Enter %d numbers here for your rows and columns:\n", IMAX * JMAX);
for(int i = 0; i < IMAX; i++) {
for(int j = 0; j < JMAX; j++) {
scanf("%f",&a[i][j]);
}
}
for(int i = 0; i < IMAX; i++) {
for(int j = 0; j < JMAX; j++) {
avgrow[i] += a[i][j];
}
avgrow[i] /= JMAX;
}
for(int j = 0; j < JMAX; j++) {
for(int i = 0; i < IMAX; i++) {
avgcol[j] += a[i][j];
}
avgcol[j] /= IMAX;
}
for(int i = 0; i < IMAX; i++) {
printf("Column%d\t", i);
}
printf("Row-Average\n\n");
for(int j = 0; j < JMAX; j++) {
for (int i = 0; i < IMAX; i++) {
printf("%8.2f\t", a[i][j]);
}
printf("%8.2f\n", avgrow[j]);
}
for(int i = 0; i < IMAX; i++) {
printf("%8.2f\t", avgcol[i]);
}
return 0;
}
In addition to the rich answers already exist, I'd like to point out something about acquiring data and computing averages without much repetitions :
So define your average arrays like this :
float avgrow[IMAX] ={0};
float avgcol[JMAX] ={0};
Then in the same loop where you scanf user's entries you can simultaneously compute averages like this:
printf("Enter %d numbers here for your rows and columns:\n", IMAX*JMAX);
for(i = 0; i < IMAX; i++)
{
for(j = 0; j < JMAX; j++)
{
scanf("%f",&a[i][j]);
avgrow[i] += a[i][j]/JMAX;
avgcol[j] += a[i][j]/IMAX;
}
}
Next step is just to print out everything, and let it be automated too :)
for(i=1; i<= JMAX; i++) printf("Column%d\t\t", i);
printf("Row Average\n");
for(i=0; i<IMAX; i++)
{
for(j=0; j<JMAX; j++)
{
printf("%8.2f\t", a[i][j]);
}
printf("%8.2f\n", avgrow[i]);
}
for(i=0; i<JMAX; i++)
printf("%8.2f\t", avgcol[i]);
By the END
you have a code that computes row and col averages for any sizes. i.e. try changing IMAX or JMAX

Program in C not working

Ok, so I'm writing a program to make a 10 x 10 array filled with random numbers between 0 & 9, and (with each step organized into a function):
(a) sum the first row and print it out
(b)print out average of main diagonal (top to bottom, left to right)
(c)print out how many 0's are in the first column
(d)make more 10 x 10 arrays with random numbers between 0 & 9 and if all the values in the main
diagonal (top to bottom, left to right) are 7 or greater, print out the array, and the
amount of tries it took. If it can't do it in 1,000,000 attempts, print that it could not
be done.
(e)make a 1D dynamically allocated array containing 10 numbers between -10 & +10, multiply by
the first array made and display the resulting vector
Can't figure out what's not making it work, getting the wrong values for all the steps when they're printed out :'( and some errors
void simple_array(int ten_by_ten[10][10])
{
int i, j;
printf("\n");
for (i=0; i<10; ++i)
{
for (j=0; j<10; ++j)
{
ten_by_ten[i][j] = rand() % 10;
printf("%d ", ten_by_ten[i][j]);
}
printf("\n");
}
}
void sum_first_row(int y[10][10])
{
int i = 0, j, sum_row = 0;
for (j=0; j<10; ++j)
{
sum_row += y[i][j];
}
printf("\nThe sum of the first row is: %d\n", sum_row);
}
void average_main_diagonal(int z[10][10])
{
int i, j = 0, average_diagonal = 0;
for (i=0; i<10; ++i)
{
++j;
average_diagonal += z[i][j];
}
printf("\nThe average of the diagonal is: %lf\n", (average_diagonal / 10.0));
}
void zeros(int a[10][10])
{
int i, j = 0, zeroz = 0;
for (i=0; i<10; ++i)
{
if (a[i][j] == 0)
++zeroz;
}
printf("\nThere are %d zero's in the first column\n", zeroz);
}
void multiple_arrays()
{
int sum_diagonal = 0,array[10][10], i, j, k, l, c;
while ((sum_diagonal < 70) && (c <= 1000000))
{
j = 0;
k = 0;
l = 0;
i = 0;
for (i=0; i<10; ++i)
{
for (j=0; j<10; ++j)
{
array[i][j] = rand() % 10;
}
}
for (k=0; k<10; ++k)
{
++l;
sum_diagonal += array[k][l];
}
++c;
}
if (c = 1000000)
printf("\nCould not get a diagonal with numbers >= 7\n");
else
{
j = 0;
i = 0;
for (i=0; i<10; ++i);
{
printf("\n");
for (j=0; j<10; ++j)
printf("%d ", array[i][j]);
}
printf("It took %d many tries to get a diagonal with all numbers >= 7", c);
}
}
void array_multiplication(int b)
{
int **arrays, i, j, k, l, m, prod[10];
arrays = (int **) calloc (10, sizeof(int *));
for (i=0; i<10; ++i)
arrays[i] = (int *) calloc (1, sizeof(int));
for (i=0; i<10; i=i+1)
{
arrays[i] = (rand() % 21) -10;
}
for (k=0; k<10; ++k)
{
prod[k] = 0;
for (l=0; l<10; ++l)
prod[k] = prod[k] + b[k][l] * arrays[l];
}
printf ("The product is: <");
for (m=0; m<10; ++m)
printf ("%d, ", prod[m]);
printf (">\n");
}
int main()
{
int x[10][10];
simple_array(x);
sum_first_row(x)
average_main_diagonal(x);
zeros(x);
multiple_arrays();
array_multiplication(x);
return (0);
}
Getting the following errors:
When I comment out the "array multiplication" function (because it's getting the following errors: (a) assignment makes pointer from integer without cast "arrays[i] = (rand() % 21) -10;" (b) value is neither array nor pointer "prod[k] = prod[k] + b[k][l] * arrays[l];" (c) passing arg1 of "array_multiplication" makes integer from pointer without cast "array_multiplication(x);"
and it prints out an incorrect average of the diagonal
Help is Extremely appreciated!!!
Thanks,
--Rob
arrays[i] = (rand() % 21) -10; : arrays[i] is a pointer. You can't assign an integer to a pointer. Actually you just assigned some allocated memory to arrays[i] on the previous line, so even if this worked you would leak that memory.
Maybe you meant to have two nested loops, if you want to put a value in every row and every column?
Also you never free the memory you calloc'd. And don't cast the value returned by calloc either.
The other errors, you meant to declare your function as
void array_multiplication(int b[10][10])
instead of (int b). It is complaining that you are doing array operations on an int.
Your code that looks at the diagonals is incorrect (which you knew). You are incrementing "j" before using it...and so "i" is 0 and "j" is 1...which is not the diagonal. You either need to do your "++j" AFTER using it to look up the value....but would be better suited to just use z[i][i] (use "i" for BOTH indices).
This problem happens in both average_main_diagonal and multiple_arrays.

Glitch initialising matrix in C

I am trying to create a matrix in C in order to solve the LaPlacian
However, this is going wrong. I have located the problem at the initialisation stage of the matrix. Every time the program is run, it places a seemingly random value in one element. This number changes each time suggesting an instability in the code, and happens if the matrix is made to be greater than 9x9.
Why could this be happening?
Thanks
The code I am using to show the matrix is:
// create matrix
double mat[meshno][meshno] = { {0} }; // x direction defined by i, y by j
//variables
int i, j, k;
//print initial matrix
for (i=0;i<=meshno;i++)
{
for(j=0;j<= meshno;j++)
{
printf("%f ", mat[i][j]);
}
printf("\n");
}
You are looking at an element past the end of each array.
Your for loops should use < and not <=:
for (i = 0; i < meshno; ++i)
{
for(j = 0; j < meshno; ++j)
{
Change to
for (i=0; i< meshno;i++)
{
for(j=0; j< meshno;j++)
{

Resources