0

I filled the first column with random numbers in the range of [20,25], now I want the user to fill the second column with random numbers in the range of [20,25]. How can I do that?

#include <stdio.h> void main() { int Temperature[5][2] = {{20},{21},{22},{23},{24}}; printf("I created a 2D array of size 5x2,"); printf("and I filled the first column with random values in the range [20,25]\n"); for(int i=0; i<5; i++) { printf("%d ",Temperature[i][0]); printf("\n"); } printf("Please fill the second column with values in the range [0,20]\n"); int i, j; for(j=0;j<5;j++) { printf("Value[%d]:",j); scanf("%d", &Temperature[0][j]); } } 
1
  • Try this: scanf("%d", &Temperature[j][1]); Commented May 9, 2020 at 13:08

3 Answers 3

2

For a two dimensional array in C, array[x][y] ---> denotes xth row and yth column

Since arrays are 0-indexed, 2nd column in your example implies column number should be 1 (0 is the first column)

Modified code

#include <stdio.h> void main() { int Temperature[5][2]={{20},{21},{22},{23},{24}}; printf("I created a 2D array of size 5x2,"); printf("and I filled the first column with random values in the range [20,25]\n"); for(int i=0; i<5; i++) { //correction done here printf("%d ",Temperature[i][0]); printf("\n"); } printf("Please fill the second column with values in the range [0,20]\n"); int i, j; for(j=0;j<5;j++) { printf("Value[%d]:",j); scanf("%d", &Temperature[j][1]); } } 
Sign up to request clarification or add additional context in comments.

14 Comments

thank you so much, can you help me in printing the array
printf("Two Dimensional array elements:\n"); for(i=0; i<5; i++) { for(j=0;j<2;j++) { printf("%d\n ", Temperature[i][j]); } }
x -> row y -> column
@keskinsaf I followed the print statement in the asker's first printf statement: printf("I created a 2D array of size 5x2,"); Also the asker has declared the array of size 5x2
|
1
&Temperature[0][j] 

The actual element would be under Temperature[j][1].

4 Comments

thank you, plus do you know how can i print the array as 2 columns after that?
printf("Two Dimensional array elements:\n"); for(i=0; i<5; i++) { for(j=0;j<2;j++) { printf("%d\n ", Temperature[i][j]); } }
this is what i did
for(i=0; i<5; i++) printf("%d %d\n", Temperature[i][0], Temperature[i][1]);
0

I have modified your code a little bit assuming that you expect the user input numbers between 20 and 25, and added some logs in order to help your understanding.

#include <stdio.h> int main() { int i, Temperature[5][2] = {{20}, {21}, {22}, {23}, {24}}; printf("I created a 2D array of size 5x2,"); printf("and I filled the first column with random values in the range [20,25] (inclusive)\n"); for (i = 0; i < 5; i++) { printf("%d ", Temperature[i][0]); printf("\n"); } printf("\nPlease fill the second column with values in the range [20,25] (inclusive)\n"); for (i = 0; i < 5; i++) { do { printf("Value[%d]:", i); scanf("%d", &Temperature[i][1]); } while (Temperature[i][1] < 20 || Temperature[i][1] > 25); } printf("and the user filled the second column with values in the range [20,25] (inclusive)\n"); for (i = 0; i < 5; i++) { printf("%d \n", Temperature[i][1]); } } 

Note that values are not random, first column consists of the values I initialized and the second column consists of the values user entered.

2 Comments

thank you so much, i should add an if statement in the loop to make sure the user entered values between 20 and 25 right? and how can i print that array ?
I printed that array at the end of execution. You can run and see it. And yes, if you want the user input to be between some specific values, you should check user input. I did that in another loop ( do - while ) to force to the user to input valid values until my expectation is satisfied.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.