11B. Simple C++ Programming Two Dimensional Array Compile and run the following
ID: 3939353 • Letter: 1
Question
11B. Simple C++ Programming Two Dimensional Array
Compile and run the following program to understand what it does. // This progra m will ask a runner for her/his fastest 3 times for 4 // different distances and will display them using a 2-D array. #include using namespace std /a function that returns a distance based on the choice i int find_distance(int i int main() int i, j double data [4][3 I/This arraywill keep 12 values in 4 rows and 3 columns // 4 events and 3 times for each one of the events for(i :0; iExplanation / Answer
#include<bits/stdc++.h>
using namespace std;
int find_distance(int i)
{
switch(i)
{
case 0:
//100 m
return 100;
break;
case 1:
//150 m
return 150;
break;
case 2:
//200 m
return 200;
break;
default ://400m
return 400;
}
}
int main(int argc, char const *argv[])
{
int i,j;
int distance[4];
double data[4][3]; //this array will keep 12 values in 4 rows and 3 column
//4 events and 3 times for each time
for(i=0;i<4;i++)
{
distance[i]=find_distance(i);
cout<<" Enter your 3 best runing times for"<<distance[i]<<"m ";
for(j=0;j<3;j++)
{
cout<<"Enter a time (in seconds):";
cin>>data[i][j];
}
}
cout<<"Here are your 3 best times:";
for ( i = 0; i < 4; ++i)
{
cout<<" Distance : "<<distance[i]<<"m ";
for (j = 0; j < 3; ++j)
{
cout<<data[i][j]<<" ";
}
cout<<endl;
}
for ( i = 0; i < 4; ++i)
{
cout<<" Distance : "<<distance[i]<<"m ";
double max=0;
double min=9999;
for (j = 0; j < 3; ++j)
{
if(max<data[i][j])
max=data[i][j];
if(min>data[i][j])
min=data[i][j];
}
cout<<"Worst time "<<max<<endl;
cout<<"Best time "<<min<<endl;
cout<<endl;
}
return 0;
}
==========================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ dist.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter your 3 best runing times for100m
Enter a time (in seconds):1
Enter a time (in seconds):2
Enter a time (in seconds):3
Enter your 3 best runing times for150m
Enter a time (in seconds):4
Enter a time (in seconds):5
Enter a time (in seconds):6
Enter your 3 best runing times for200m
Enter a time (in seconds):7
Enter a time (in seconds):8
Enter a time (in seconds):9
Enter your 3 best runing times for400m
Enter a time (in seconds):10
Enter a time (in seconds):11
Enter a time (in seconds):12
Here are your 3 best times:
Distance : 100m
1 2 3
Distance : 150m
4 5 6
Distance : 200m
7 8 9
Distance : 400m
10 11 12
Distance : 100m
Worst time 3
Best time 1
Distance : 150m
Worst time 6
Best time 4
Distance : 200m
Worst time 9
Best time 7
Distance : 400m
Worst time 12
Best time 10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.