I am writing a C++ program that selects the values of a 4 X 5 array of integers
ID: 3551637 • Letter: I
Question
I am writing a C++ program that selects the values of a 4 X 5 array of integers in increasing order and stores the selected values to a one dimensional array. I am having a really hard time of it. Would someone mind looking at my program and let me know what I am doing wrong? Thanks!
Here's my program so far:
#include <cstdlib>
#include <iostream> //pre-processor directs - iostream talks to screen
#include <iomanip> //pre-processor directs - string
#include <cmath> //allows math functions
using namespace std; //introduces namespace
const int size = 50;
int main()
{
int Value[4][5]={16,22,99,4,18,-258,4,101,5,98,105,6,15,2,45,33,88,72,16,3};
int i, j, sort[size]; //Declares array
cout<<"The elements in Value are: "<<endl;
for (int i=0;i<4;i++)
{
cout<<endl;
for (int j=0;j<5;j++)
{
cout<<" "<<Value[i][j];
for (int k=0;k<size;k++)
{
sort[k] = Value[i][j];
cout<<" "<<sort[k];
}
}
}
cout<<endl;
return 0;
}
Explanation / Answer
#include <cstdlib>
#include <iostream> //pre-processor directs - iostream talks to screen
#include <iomanip> //pre-processor directs - string
#include <cmath> //allows math functions
using namespace std; //introduces namespace
const int size = 50;
int main()
{
int Value[4][5]={16,22,99,4,18,-258,4,101,5,98,105,6,15,2,45,33,88,72,16,3};
int i, j, sort[size],t=0; //Declares array
cout<<"The elements in Value are: "<<endl;
for (int i=0;i<4;i++)
{
cout<<endl;
for (int j=0;j<5;j++)
{
cout<<" "<<Value[i][j];
sort[t++] = Value[i][j];
}
}
cout<<endl;
cout<<"before sorting --"<<endl;
for (int k=0;k<t;k++)
cout<<" "<<sort[k];
for(i=0;i<t-1;i++)
for(j=0;j<t-1;j++)
{if(sort[j]>sort[j+1])
sort[j+1]=sort[j]+sort[j+1]-(sort[j]=sort[j+1]);
}
cout<<endl<<"after sorting --"<<endl;
for (int k=0;k<t;k++)
cout<<" "<<sort[k];
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.