Write a program to store all integer values shown below into a two-dimensional a
ID: 3535795 • Letter: W
Question
Write a program to store all integer values shown below into a two-dimensional array as following;
int val[3][4] = {8,-16,9,52,-3,-15,27,6,14,-25,-2,10};
Then select all positive numbers and put them into a vector. Sort, and display the median value
I cant figure ^ part out.............
#include <iostream>
using namespace std;
int main ()
{
// an array with 5 rows and 2 columns.
int a[3][4] = {8,-16,9,52,-3,-15,27,6,14,-25,-2,10};
// output each array element's value
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main ()
{
// an array with 5 rows and 2 columns.
int a[3][4] = {8,-16,9,52,-3,-15,27,6,14,-25,-2,10};
// for positive integer
int vector[3][4],x=0,y=0;
for(i=0;i<5;i++)
{
for(i=0;i<5;i++)
{
if(y>2)
x++;
for(j=0;j<2;j++)
{
if(a[i][j]>0)
{
vector[x][y]=a[i][j];
y++;
}
//sorting
for(i=0;i<5;i++)
{
for(j=0;j<2;j++)
{
if(vector[i][j]>vector[i][j+1])
{
x=vector[i][j];
vector[i][j]=vector[i][j+1];
vector[i][j+1]=x;
}
}
}
// output each array element's value
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.