Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

Q1.Write a function which accepts an array of size 5 rows by 5 columns, and allows the user to input values in the array. Q2.Write a function which finds the difference between the largest element and the smallest element in an array of size 5 rows by 5 columns. in C language.

OpenStudy (anonymous):

@TuringTest

OpenStudy (anonymous):

Q1: #include<stdio.h> int main() { int array[5] [5]; int i, j; for(i=0; i<5; i++) { printf("Input values:", i); for(j=0, j<5; j++) scanf("%d", &array[i][j]): } return 0; } Can you please correct my work. for question 2, i have no idea. Help pls.

OpenStudy (turingtest):

the first part is fine, but I think it's a bit strange to have the print statement occur only on each new row. I suggest the more user friendly slight adjustment: printf("Input 5 values in row %d", i);

OpenStudy (turingtest):

for the second part you will need to initialize two variables: one to keep track of the biggest number seen so far, on to keep track of the smallest

OpenStudy (anonymous):

can you give me the code for q2.

OpenStudy (turingtest):

I have to stop you right there. OpenStudy is not about doing people's homework for them, it's about teaching. Giving answers directly is against the code of conduct. I'm more than happy to help you with concepts and pseudocode, but I won't give a final answer unless I'm just correcting some typos on code you already wrote.

OpenStudy (turingtest):

anyway, then all you do is subtract them at the end and return that value #include<stdio.h> int main() { int array[5] [5]; int i, j; //initialize 2 variables for the biggest element and smallest element for(i=0; i<5; i++) { printf("Input 5 values in row %d", i); for(j=0, j<5; j++) scanf("%d", &array[i][j]): //if the element we are currently bigger than the biggest seen so far //update big to that value //if this element is smaller than the smallest seen so far //update small to that value } //print big minus small return 0; }

OpenStudy (turingtest):

all you need to do is modify your code according to the pseudocode I provided. If you have any specific questions about what I wrote please let me know

OpenStudy (anonymous):

thank you dear, i will try to modify my code. :)

OpenStudy (turingtest):

you're welcome :)

OpenStudy (anonymous):

@TuringTest I have tried to write the code, i am stuck in the "update big and small value". #include<stdio.h> int main() { int array[5][5]; int i, j; int biggest, smallest, difference; for(i=0; i<5; i++) { printf("Input 5 values in row %d", i); for(j=0; j<5; j++) scanf("%d", &array[i][j]); if(array[i][j]>array[biggest]) { biggest= array[i][j] // update big to what value? } else(array[i][j]<array[smallest]) smallest= array[i][j] //update small to what value? } difference=biggest-smallest; printf("The difference between the largest element and smallest element is %d", difference); return 0; }

OpenStudy (turingtest):

you should initialize biggest and smallest to be the first element in the array, otherwise when you try to compare it to another element in the array you have no idea what number you are comparing. Since this program takes the elements in the array as input from the user, you should initialize the variables AFTER taking in the values of the array/ Also, biggest and smallest represent the smallest *element*, not their indices, so why are you comparing array[i][j] with array[biggest] ? You should be comparing array[i][j] with biggest (or smallest). If the biggest element is 10, then array[biggest] is outside the array! Keep in mind what biggest and smallest actually represent. #include<stdio.h> int main() { int array[5][5]; int i, j, difference; for(i=0; i<5; i++) { printf("Input 5 values in row %d", i); for(j=0; j<5; j++) scanf("%d", &array[i][j]); } //now initialize biggest and smallest to the first element in the array if(array[i][j]>array[biggest]) //compare array[i][j] with the element biggest { //array[biggest] is just some element in the array that has the index # //biggest, which if it is larger than 5 doesn't even fit in the array! biggest= array[i][j]; } else(array[i][j]<array[smallest]) //same problem as I stated earlier about the index //and this should be an "else if" statement, //since it is another conditional { smallest= array[i][j]; } difference=biggest-smallest; printf("The difference between the largest element and smallest element is %d", difference); return 0; }

OpenStudy (turingtest):

you should initialize biggest and smallest to be the first element in the array, otherwise when you try to compare it to another element in the array you have no idea what number you are comparing. Since this program takes the elements in the array as input from the user, you should initialize the variables AFTER taking in the values of the array/ Also, biggest and smallest represent the smallest *element*, not their indices, so why are you comparing array[i][j] with array[biggest] ? You should be comparing array[i][j] with biggest (or smallest). If the biggest element is 10, then array[biggest] is outside the array! Keep in mind what biggest and smallest actually represent. #include<stdio.h> int main() { int array[5][5]; int i, j, difference; for(i=0; i<5; i++) { printf("Input 5 values in row %d", i); for(j=0; j<5; j++) scanf("%d", &array[i][j]); } //now initialize biggest and smallest to the first element in the array //then go through each element again as you did when getting the inputs //with a double for loop, and inside that you put the following checks: //i.e for(i=0;i<5;i++)... /// for(j=0;j<5;j++)... { if(array[i][j]>array[biggest]) //compare array[i][j] with the element biggest { //array[biggest] is just some element in the array that has the index # //biggest, which if it is larger than 5 doesn't even fit in the array! biggest= array[i][j]; } else(array[i][j]<array[smallest]) //same problem as I stated earlier about the index //and this should be an "else if" statement, //since it is another conditional { smallest= array[i][j]; } } difference=biggest-smallest; printf("The difference between the largest element and smallest element is %d", difference); return 0; }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!