In C language, for each question chose answers 0 to 15. Please show how you test
ID: 3734076 • Letter: I
Question
In C language, for each question chose answers 0 to 15. Please show how you tested each question with code.
Use the following program for problems 1-2
#include<stdio.h>
int main(){
int n;
for (n = 12; n>=5; n++){
printf("%d, ",n);
n = n – 4; }
return 0; }
1. How many iterations of the loop occur?
2. What is the last value printed out?
__________________________________________________________________________________
Consider the following code snippet for problems 3-5
int i;
int ar[7] = {5, 12, 4, 0, 1, 7, 2};
for (i=1;i<6;i++) {
if (ar[i-1] > ar[i]) {
ar[i] = ar[i-1];
ar[i-1] = ar[i];
}
}
3. What value is in ar[2] after this code executes?
4. What value is in ar[1] after this code executes?
5. What value is in ar[0] after this code executes?
___________________________________________________________________________________
Use the following code for problems 6-8
int row,col,count,bbval1,bbval2; count=0;
int marks[5][5]={{1,1,0,0,1},{0,1,0,0,1},
{1,1,1,0,1},{0,0,0,0,1},{1,0,1,0,1}};
for(row=0;row<5;row++) {
bbval1=0; bbval2=0;
for(col=0;col<5;col++) {
count += marks[row][col];
bbval1 += marks[row][col];
bbval2 += marks[col][row];
}
if(bbval2==5) printf("we have a winner");
if(bbval1==5) printf("we have a winner");
if(count==5) printf("we have a winner");
}
printf("count %d ",count);
printf("bbval1 %d ",bbval1);
printf("bbval2 %d ",bbval2);
6. How many times does “we have a winner” get
printed?
7. What value is displayed for count?
8. What value is displayed for bbval1?
___________________________________________________________________________________
Consider the following program for problems 9-11
#include<stdio.h>
int func1(int *val1, int val2) {
int tmp = val2;
val2 = *val1;
*val1 = tmp;
return val2 - *val1;
}
main(){
int val2 = 10;
int val1 = 5;
int val3 = func1(&val2,val1);
printf("val1 %d val2 %d val3 %d ",
val1, val2, val3);
return 0;}
9. What is val1 after func1 is called?
10. What is val2 after func1 is called?
11. What is val3 after func1 is called?
___________________________________________________________________________________
Use the following code and input for problems 12-13.
int i,j;
int numrows,numcols;
int ourGrid[10][10];
scanf("%d %d ", &numrows, &numcols);
for(i=0;i<numrows;i++){
for(j=0;j<numcols;j++){
scanf("%d",&(ourGrid[i][j]));
} }
Input:
3 4 7 10 14 15 5 8 -3 4 -2 11 16 -13 9 18 -5
12. How many values were stored in ourGrid?
13. What value is in position ourGrid[0][0]?
___________________________________________________________________________________
Use the following code and input for problems 14-15.
#include<stdio.h>
int myfunc(int mat[4][4],int rows,int cols,
int m){
int k;
int sum=0;
for (k=0;k<rows;k++){
sum+= mat[k][m]; }
return sum; }
int main() {
int aGrid[4][4]={ {1, -2, 3, 4},
{5, 6, -7, 8},
{-4, 5, 3, 12},
{4, -2, 6, 1}};
int val = myfunc(aGrid,2,3,1);
printf("%d",val);
return 0; }
14. How many iterations are performed by the loop in
myfunc?
15. What value is returned by myfunc?
Explanation / Answer
If you post more than 1 question, as pe chegg guidelines I have to solve only first question.
Ques 1. Answer : 3
After each iteration, n effctively reduces by 3 as it is first decremented by 4 and the incremented by 1.
So, n decreases as 12 , 9, 6 and after that we break out of loop.
So, it runs 3 times.
Ques 2. Answer : 6
After each iteration, n effctively reduces by 3 as it is first decremented by 4 and the incremented by 1.
So, n decreases as 12 , 9, 6
So, the last value of n is 6 and after that it breaks out of loop.