Hello, I keep getting the maximum value to display 0 instead of the actual value
ID: 3627671 • Letter: H
Question
Hello,
I keep getting the maximum value to display 0 instead of the actual value. Please help me to fix this either in the main function or find_max function. Thank you.
Here is my c++ coding:
#include
#include
using
void
// Void function declaration for input arrays
int
// Void function declaration for finding largest number
int
{
//declare varriables
int
//declare an array for whole numbers that may contain 20 rows and 15 columns
int
cout<<
"Please enter the dimensions of the 2D Array."<<endl;
//getting number rows and columns from user
cout<<
"Please enter the number of rows:"
<<endl;
cin>>rows;
while
//confirming number of rows are within limit
{
cout<<
<<endl;
cout<<
<<endl;
cin>>rows;
}
cout<<
<<endl;
cin>>cols;
while
//confirming number ofcolumns are within limit
{
cout<<
<<endl;
cout<<
cin>>cols;
}
Input_Array(a,rows,cols);
//call function to input array
Print_2D_Array(a,rows,cols);
//call function to print array
cout<<
"The highest value is: "<<max<<endl;
//display highest value
return
}
void
{
int i,j;
// i: row & j:column in each row
for
//for each row
for(j=0;j<c;j++)
//for each column in each row
{cout<<
//ask for the array
cin>>a[i][j];
//get the value
}
}
int
//Function finding max value
{
int u,v;
for (u=0; u<ro ; u++){
if (a[u][v] > max_value){
max_value = a[u][v]; }
}
}
}
void
Print_2D_Array(int arr1[][15], int r, int c)
//function to print a 2D integer array of up to 15 columns
{
int
i = 0, j;
while
(i < r)
{
j = 0;
while
(j < c)
{
cout<<setw(5)<<arr1[i][j];
j++;
}
cout<<endl;
i++;
}
}
int max_value=0;
Explanation / Answer
Next time you paste your code do it from plain text or word format. The problem you had was that you were calling the find max function before the the array even had values within it. The changes are in red#include<iostream> #include<iomanip> using namespace std; void Print_2D_Array(int[][15],int,int);//Void function declaration for printing array void Input_Array(int[][15],int,int); //Void function declaration for input arrays int Find_Max(int[][15],int,int); //Void function declaration for finding largest number int main() { int rows=0,cols=0;//declare varriables int a[20][15]; //declare an array for whole numbers that may contain 20 rows and 15 columns int max; cout<<"Please enter the dimensions of the 2DArray."<<endl;//getting number rows and columns from user cout<<"Please enter the number of rows:"<<endl; cin>>rows; while(rows<=0||rows>20)//confirming number of rows are within limit { cout<<"The number of rows must be greater than 0 and less than or equal to 20"<<endl; cout<<"Please enter the number of rows you want to use"<<endl; cin>>rows; } cout<<"Please enter the number of columns:"<<endl; cin>>cols; while(cols<=0||cols>15)//confirming number of columns are within limit { cout<<"The number of columns must be greater than 0 and less than or equal to 15"<<endl; cout<<"Please enter the number of columns you want to use"<<endl; cin>>cols; } Input_Array(a,rows,cols);//call function to input array Print_2D_Array(a,rows,cols);//call function to print array max=Find_Max(a,rows,cols); cout<<"The highest value is:"<<max<<endl;//display highest value system("pause"); return 0; } void Input_Array(int a[][15],int r,int c) { int i,j;// i: row & j:column in each row for(i=0;i<r;i++) //for each row for(j=0;j<c;j++) //for each column in each row {cout<<"Please enter array("<<i+1<<")("<<j+1<<"):"; //ask for the array cin>>a[i][j]; //get the value } } int Find_Max(int a[][15], int ro, int co) //Function finding max value { int u,v =0; int max_value=0; for (u=0; u<ro ; u++) { for (v=0; v<co ; v++) { if (a[u][v] > max_value) { max_value = a[u][v]; } } } return max_value; } void Print_2D_Array(int arr1[][15], int r, int c) //function to print a 2D integer array of up to 15 columns { int i = 0, j; while(i < r) { j = 0; while(j < c) { cout<<setw(5)<<arr1[i][j]; j++; } cout<<endl; i++; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.