Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Whats wrong with the following statement, and why? (char c[10] = I am a stude

ID: 649665 • Letter: 1

Question

1. Whats wrong with the following statement, and why? (char c[10] = I am a student;

2. What value will the following program output? Explain in steps.

#include<stdio.h>

int main()

{

int i, k, a[10], p[3];

k = 5;

for ( i = 0; i < 10; i++) {

a[i] = i;

} for ( i = 0; i < 3; i++)

{ p[i] = a[i*(i+1)];

}

for ( i = 0; i < 3; i++)

{ k += p[i]*2;

}

printf(%d, k);

return 0;

}

3.What does the following program do? Explain why.

#include<stdio.h>

#define SIZE 10

int WhatIsThis(const int b[], int p);

int main( void )

{

int x; int a[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

x = WhatIsThis(a, SIZE);

printf(Result is %d, x);

}

int WhatIsThis( const int b[], int p)

{

if ( p == 1) { return b[0]; }

else {

return b[p-1] + WhatIsThis(b, p - 1);

}

}

4.  Assume we have the following three arrays: (65 points)

int square[ 5 ][ 5 ] = { 0 },

int product[ 5 ][ 6 ];

int table[ 5 ] [ 6 ] = { { 1, 2, 3, 4, 5 }, { 2, 4, 6, 8, 10 }, { 20, 10, 5, 3, 1 }, { 3, 6, 9, 12, 15 } }

a. Write a function to fill array square as shown below: (find the pattern and use iterations, please dont fill the array directly like a[2][2] = 9)

The picture is a 5x5 square with 1,4, 9, 16, 25 going down digaonally from the top left corner to the bottom right

b. write a function to fill array square as shown below: (find the pattern and use iterations, please dont fill the array directly like a[2][2] = 13)

The picture is a 5x5 square going in order from left to right 1,2,3,4,5,

6,7,8,9,10 etc

c. The partially initialized array "table" can be viewed as a primitive spreadsheet, in which the last column and bottom row have been left blank. Write the code to fill in this row and column with the totals of each column, each row, and the grand total.

d. Write the code to calculate the product of "square" times "table" and put the result in "product".

5. An element of a matrix is said to be Saddle Point if it is the strictly largest element (max) in its row and the strictly smallest element (min) in its column. For example, the matrix shown below has 2 saddle points:

Please write a C program that ask the user to input any matrix (size of matrix, and all elements in matrix), and output any saddle point(s) in the matrix (output the value and position of the saddle points).

Explanation / Answer

Here is the answer for all the parts. Please do rate the answer if it helped. Thank you very much.

Part 1.
char c[10] = "I am a student";

In the above statement, the char array c can only hold 10 characters including null terminator.
But it is assigned a string value with more than 10 characters.

======
Part 2.
======
First k is initialized to 5.
Next, in the following for loop
for ( i = 0; i < 10; i++) {
a[i] = i;
}

The array a[] is initialized with values from 0 -9
a[0] = 0
a[1] = 1
a[2] = 2 and so on
till a[9] = 9

The next for loop, sets the 3 values for array p
for ( i = 0; i < 3; i++)
{
p[i] = a[i*(i+1)];
}

p[0] = a[0*1] = a[0] = 0
p[1] = a[1 *2 ] = a[2] = 2
p[2] = a[2 * 3] = a[6] = 6

The next for loop changes the value of k in each iteration. To start with k was initially 5
k += p[0] * 2 i.e. k+= 0*2 i.e k += 0 . So k still is 5
k += p[1] * 2 i.e k+=2*2 i.e. k += 4 . so k = 5+4 i.e k = 9
k += p[2] * 2 i.e k += 6 * 2 i.e. k+= 12 . so k = 9 + 12 = 21

So finally the value of k is displayed in printf and the output will be 21

======
Part 3:
======

To understand the program, let us first understand the function WhatIsThis()
WhatIsThis() is a recursive function, that basically adds first p elements elements in the array.
The recursion terminates when p = 1 ie. when p =1, only the 1st element value is returned. For other
values of p, recursive calls are made to add the element in the corresponding index with value of
recursive call to add p-1 elements.

Now coming back to main(),
The int array 'a' is initialized with 10 elements 1-10 and the function WhatIsThis() is called with p = SIZE
i.e. We are asking for sum of starting 10 numbers in the array. Since there are only 10 numbers in the array,
the call will return the sum of elements 1,2,....10 which is 55. This is value is printed using printf.

So the output of this part of the code is 55.

======
Part 4
======
The various functions described in Part 4 are coded and called in main() function in the below program.
display() is just a helper function to display a matrix.

#include<stdio.h>

/*fills the square matrix of size 5x5 along the diagonal. The diagonal elements are squares of corresponding row/column number*/
void fill_diagonal(int square[][5])
{
int i;
for(i=0;i<5;i++)
{
square[i][i] = (i+1) * (i+1); //store the square of the row/col number
}
}

/*fills the square matrix of size 5x5 with sequence starting as 1,2,3...*/
void fill_sequence(int square[][5])
{
int i,j,k;
for(i=0,k=1; i<5; i++)
{
for(j=0; j<5; j++, k++)
square[i][j] = k;
}
}


/*calculates the total across rows and columns in the table*/
void calculate_total(int table[][6])
{
int i,j,sum;
int row=5, col=6;

/*calcualte total for each colum and store values in correspondin col of last row*/
for(j=0; j<col-1; j++)
{
sum = 0;
for(i=0; i<row-1; i++)
{
sum+=table[i][j];
}
table[row-1][j] = sum;
}

/*calcualte total for each row and store values in correspondin row of last column*/
for(i=0; i<row; i++)
{
sum = 0;
for(j=0; j<col-1; j++)
{
sum+=table[i][j];
}
table[i][col-1] = sum;
}
}

/*calculates the product of 2 matrices
first matrix is of size m1xn1
second matrix is of size m2xn2
and product matrix is of size m1xn2
*/
void calculate_product(int m1, int n1, int first[m1][n1], int m2, int n2, int second[m2][n2],int product[m1][n2])
{
int i,j,k,prod;
for(i=0; i<m1; i++)
{
prod=0;
for(j=0; j<n2; j++)
{
for(k=0; k<n1; k++)
{
prod += first[i][k] * second [k][j];
}
product[i][j] = prod;
}
}
}


/*displays a matrix with have row number of rows and col number of columns*/
void display(int row, int col, int mat[row][col])
{
int i,j;
for(i=0; i<row; i++)
{
printf(" ");
for(int j=0; j<col; j++)
{
printf(" %d",mat[i][j]);
}
}
printf(" ");
}


int main( void )
{
int square[ 5 ][ 5 ] = { 0 };
int product[ 5 ][ 6 ];
int table[ 5 ] [ 6 ] = { { 1, 2, 3, 4, 5 }, { 2, 4, 6, 8, 10 }, { 20, 10, 5, 3, 1 }, { 3, 6, 9, 12, 15 } };
printf(" The square matrix before using fill_diagonal is ");
display( 5,5, square);

fill_diagonal(&square[0]);
printf(" The square matrix after using fill_diagonal is ");
display(5,5, square);

fill_sequence(square);
printf(" The square matrix after using fill_sequence is ");
display( 5,5,square);


printf(" The table matrix before calculating total is ");
display(5,6,table);

calculate_total(table);
printf(" The table matrix after calculating row and column totals is ");
display(5,6,table);

calculate_product(5,5,square,5,6,table,product);
printf("The product of square and table is ");
display(5,6,product);
}


output
======
The square matrix before using fill_diagonal is
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0

The square matrix after using fill_diagonal is
   1   0   0   0   0
   0   4   0   0   0
   0   0   9   0   0
   0   0   0   16   0
   0   0   0   0   25

The square matrix after using fill_sequence is
   1   2   3   4   5
   6   7   8   9   10
   11   12   13   14   15
   16   17   18   19   20
   21   22   23   24   25

The table matrix before calculating total is
   1   2   3   4   5   0
   2   4   6   8   10   0
   20   10   5   3   1   0
   3   6   9   12   15   0
   0   0   0   0   0   0

The table matrix after calculating row and column totals is
   1   2   3   4   5   15
   2   4   6   8   10   30
   20   10   5   3   1   39
   3   6   9   12   15   45
   26   22   23   27   31   129
The product of square and table is
   207   381   562   774   1017   2034
   467   861   1272   1754   2307   4614
   727   1341   1982   2734   3597   7194
   987   1821   2692   3714   4887   9774
   1247   2301   3402   4694   6177   12354

=======
part 5
=======

program to find saddle point maximum in row and minimum in column

#include <stdio.h>
#define MAX 10
int row_max(int rowNo, int row,int col, int mat[MAX][MAX])
{
int i;
int max=mat[rowNo][0];
for(i=1; i<col; i++)
if(mat[rowNo][i] > max)
max = mat[rowNo][i];

return max;
}
int col_min(int colNo, int row,int col, int mat[MAX][MAX])
{
int i;
int min=mat[0][colNo];
for(i=1; i<row; i++)
if(mat[i][colNo] < min)
min = mat[i][colNo];

return min;
}

void display_saddle(int row, int col, int mat[MAX][MAX])
{
int i, j, max[row], min[col];
int count=0;

for( i=0; i<row; i++ )
max[i] = row_max(i, row, col, mat);

for( i=0; i<col; i++ )
min[i] = col_min(i, row, col, mat);

for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
if(mat[i][j] == max[i] && mat[i][j] == min[j])
{
printf(" %d is a maximum in its row (%d) and minimum in its column(%d) ", mat[i][j], i, j);
count++;
}
}
}

if(count==0)
printf(" There are no saddle points! ");

printf(" ");

}

/*displays a matrix with have row number of rows and col number of columns*/
void display(int row, int col, int mat[MAX][MAX])
{
int i,j;
for(i=0; i<row; i++)
{
printf(" ");
for(j=0; j<col; j++)
{
printf(" %d",mat[i][j]);
}
}
printf(" ");
}
int main()
{
int mat[MAX][MAX];
int row, col, i, j;
printf("How many rows? ");
scanf("%d",&row);
printf("How many cols? ");
scanf("%d",&col);

printf("Enter the matrix elements ");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d",&mat[i][j]);

}
}
printf(" The given matrix is ");
display(row, col, mat);
printf(" Finding saddle points in matrix...");
display_saddle(row,col, mat);

return 0;
}

output
======
How many rows? 3
How many cols? 3
Enter the matrix elements
1 2 3
4 5 6
7 8 1

The given matrix is

   1   2   3
   4   5   6
   7   8   1

Finding saddle points in matrix...
There are no saddle points!

-----
How many rows? 3
How many cols? 3
Enter the matrix elements
1 2 3
4 5 6
7 8 9

The given matrix is

   1   2   3
   4   5   6
   7   8   9

Finding saddle points in matrix...
3 is a maximum in its row (0) and minimum in its column(2)