1. (2 points) Declare a two-dimensional integer array of size 10 by 5 (10 rows a
ID: 3556499 • Letter: 1
Question
1.
(2 points)
Declare a two-dimensional integer array of size 10 by 5 (10 rows and 5columns) named Table.
2.
(8 points)Using the rand( ) function initialize the elements of Table to numbersbetween 10 and 75.
3.(10 points)Print the whole array.
4.(10 points)Write a function that takes a two-dimensional integer array and printsits content.Call this function to print the content of the first 5 rows of array Table.
5.(15 points)Write a function that takes a two-dimensional integer array and prints the sum of the elements in each row.Call this function to calculate the sum of all elements in arrayTable and print the sum.
6.(15 points)Write a function that takes a two-dimensional integer array and a number and returns true if the number appears in the array and false, otherwise.Ask the user to enter a number between 10 and 75 and call this function to determine whether the number appears in the array table.
After completing all the tasks above, submit a copy of your source code and the output for your program which should include the output for tasks 3
Explanation / Answer
//For code view goto: http://ideone.com/5pwxK8
#include <iostream>
#include<stdlib.h>
#define numrows 10
#define numcols 5
using namespace std;
void printArray(int Table[numrows][numcols],int rows)
{
for(int i=0;i<rows;i++)
{
cout<<" Row "<<i+1<<":";
for(int j=0;j<numcols;j++)
cout<<Table[i][j]<<" ,";
}
}
void printRowSum(int Table[numrows][numcols])
{
for(int i=0;i<numrows;i++)
{
cout<<" Sum of Row "<<i+1<<":";
int sum=0;
for(int j=0;j<numcols;j++)
sum+=Table[i][j];
cout<<" "<<sum;
}
}
bool findNumber(int Table[numrows][numcols],int key)
{
for(int i=0;i<numrows;i++)
for(int j=0;j<numcols;j++)
if(key==Table[i][j]) return true;
return false;
}
int main() {
// your code goes here
int Table[numrows][numcols];//Q1
for(int i=0;i<numrows;i++)
for(int j=0;j<numcols;j++)
Table[i][j]=rand()%66+10; //Q2
for(int i=0;i<numrows;i++)
{
cout<<" Row "<<i+1<<":";
for(int j=0;j<numcols;j++)
cout<<Table[i][j]<<", ";}//Q3
cout<<" Printing array upto 5rows";
printArray(Table,5); //Q4
printRowSum(Table);//Q5
cout<<" Enter the no. to search:";//Q6
int key;
cin>>key;
if(findNumber(Table,key)) cout<<"Match found in the array";
else
cout<<"No match found in the array";
return 0;
}
//For code view goto: http://ideone.com/5pwxK8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.