4. Given the following declaration, what is stored in the 8th element of the arr
ID: 3889781 • Letter: 4
Question
4. Given the following declaration, what is stored in the 8th element of the array? int list[10] = {1, 2, 3, 4, 5};
5. Write a loop that finds the smallest element in an integer array called data containing 100 elements.
6. Write a C program that computes that maximum of a 2D array of size 6 by 5.
7. Write a C program that computes that total sum of a 2D array of size 6 by 5.
8. Write a C program that searches for value key in a 2D array of size 6 by 5. The program should return true if found false otherwise. (similar as showed in the class)
9. Write a C program that searches for value key in a 2D array of size 6 by 5. The program should return the row # of location the value was found at if found and return -1 if not found.
10. Write a C program that searches for value key in a 2D array of size 6 by 5. The program should return the row # and col# of location the value was found at if found and return -1 if not found.
11. Write a C program that computes that maximum of a specific row R in a 2D array of size 6 by 5.
12. Write a C program that computes that total sum of a specific column C in a 2D array of size 6 by 5.
13. Write a C program that searches for value key in a specific row R in a 2D array of size 6 by 5. The program should return true if found false otherwise.
14. You are given an array of integers of size 10. You take input from user to populate the array. Now, can you find the second smallest number in your array?
18. How to swap two numbers in an array? You get the array indices to swap from the user as input. Print the array before and after swapping. For instance, int arr[10] = {1, 2, 3, 4, 5}; Input : 0 1 Sample Output : {1, 2, 3, 4, 5} => Array before swapping {2, 1, 3, 4, 5} => Array after swapping
Explanation / Answer
Hi,
I could complete till question 10.
Ans 4 - Element 0 is stored as default element in the array as we have not assigned any value to that index hence the machine has assigned all the un assigned elements as 0.
Ans 5 -
#include <iostream>
using namespace std;
int main()
{
int array []= {4,2,3,1,5};
int smallest = array[0] ;
for ( int i=1; i < sizeof(array)/sizeof(array[0]); ++i )
if ( array[i] < smallest )
smallest = array[i] ;
cout << smallest << ' ' ;
return 0;
}
Ans 6 -
#include <iostream>
using namespace std;
int main()
{int x[6][5]={1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,24};
int max=x[0][0], min= x[0][0];
int i,j;
for (i=0; i<6; i++)
for (j=0; j<5; j++)
{
if(x[i][j]> max)
max= x[i][j];
}
cout<<max;
return 0;
}
Ans 7-
#include <iostream>
using namespace std;
int main()
{int x[6][5]={1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,24};
int max=x[0][0], min= x[0][0];
int i,j;
int sum=0;
for (i=0; i<6; i++)
for (j=0; j<5; j++)
{
sum+=x[i][j];
}
cout<<sum;
return 0;
}
Ans 8, 9,10
#include <iostream>
#define MAX_ROWS (6)
#define MAX_COLS (5)
using namespace std;
int search(int [MAX_ROWS][MAX_COLS],int val,int val1);
int main()
{int x1[6][5]={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,
26,27,28,32,12};
search(x1, 6,26);
getchar();
return 0;
}
int search(int mat[6][5], int n, int x) {
int i = 0, j =5;
while ( i < n && j >= 0 ) {
if ( mat[i][j] == x ) {
printf(" Found at %d, %d", i, j);
return 1;
}
if ( mat[i][j] > x )
j--;
else
i++;
}
printf(" Element not found");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.