CS201-VU- Multi Diemensional arrays in cpp with do while loop
Hello guys!
#include <iostream>
#include <stdlib.h>
using namespace std;
void showElements(float cov19data[][4]); // functions definitions
double percentageDeath(float cov19data[][4], int userChoice);
double percentageRecovered(float cov19data[][4], int userChoice);
int main()
{
int selectedOption;
float cov19data[7][4] // defining two diemensional array
{
{ 0, 560433, 22115, 32634},
{ 1, 156363, 19899, 34211},
{ 2, 84279, 10612, 0},
{ 3, 82160, 3341, 77663},
{ 4, 71686, 4474, 43894},
{ 5, 56956, 1198, 3446},
{ 6, 5374, 93, 1095}
};
do { // do while loop to run program untill user terminates it.
showElements(cov19data); // function call
cout << "\n\n";
cout << "Press The Country Code To Calculate Percentage Of Dead And Recovered Persons\n\n";
cout << "Press 0 for Pakistan.\n";
cout << "Press 1 for China.\n";
cout << "Press 2 for Italy.\n";
cout << "Press 3 for UK.\n";
cout << "Press 4 for Iran.\n";
cout << "Press 5 for France.\n";
cout << "Press 6 for Turkey.\n";
cout << "Press 7 to Exit.\n\n";
cout << "Please Select An Option, Use Numbers From 0 To 7: ";
cin >> selectedOption;
system("cls"); // this function is used to clear the console screen
cout << "________________________________________________\n";
if(selectedOption>7){
cout<<"choice is wrong!";
} else {
cout<<"choice Selected : "<<selectedOption<<"\n";
cout << "Percentage of death is: " << percentageDeath(cov19data, selectedOption);
cout << "\n\nPercentage of recovered is: " << percentageRecovered(cov19data, selectedOption);
}
cout <<"\n\n";
} while (true);
}
void showElements(float cov19data[][4]) // function body implementation
{
cout << "Source Data:\n\n";
cout << "Country\tCases\tDeaths\tRecovered\n\n";
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
cout << cov19data[i][j];
cout << "\t";
}
cout << "\n";
}
}
double percentageDeath(float cov19data[][4], int selectedOption)
{
double deathPercentage = (cov19data[selectedOption][2] / cov19data[selectedOption][1]) * 100.0;
return deathPercentage;
}
double percentageRecovered(float cov19data[][4], int selectedOption)
{
double recoveredPercentage = (cov19data[selectedOption][3] / cov19data[selectedOption][1]) * 100.0;
return recoveredPercentage;
}
The following cpp program demonstrates the use of two dimensional Arrays and do while loop;
comment below for any queries.
#include <iostream>
#include <stdlib.h>
using namespace std;
void showElements(float cov19data[][4]); // functions definitions
double percentageDeath(float cov19data[][4], int userChoice);
double percentageRecovered(float cov19data[][4], int userChoice);
int main()
{
int selectedOption;
float cov19data[7][4] // defining two diemensional array
{
{ 0, 560433, 22115, 32634},
{ 1, 156363, 19899, 34211},
{ 2, 84279, 10612, 0},
{ 3, 82160, 3341, 77663},
{ 4, 71686, 4474, 43894},
{ 5, 56956, 1198, 3446},
{ 6, 5374, 93, 1095}
};
do { // do while loop to run program untill user terminates it.
showElements(cov19data); // function call
cout << "\n\n";
cout << "Press The Country Code To Calculate Percentage Of Dead And Recovered Persons\n\n";
cout << "Press 0 for Pakistan.\n";
cout << "Press 1 for China.\n";
cout << "Press 2 for Italy.\n";
cout << "Press 3 for UK.\n";
cout << "Press 4 for Iran.\n";
cout << "Press 5 for France.\n";
cout << "Press 6 for Turkey.\n";
cout << "Press 7 to Exit.\n\n";
cout << "Please Select An Option, Use Numbers From 0 To 7: ";
cin >> selectedOption;
system("cls"); // this function is used to clear the console screen
cout << "________________________________________________\n";
if(selectedOption>7){
cout<<"choice is wrong!";
} else {
cout<<"choice Selected : "<<selectedOption<<"\n";
cout << "Percentage of death is: " << percentageDeath(cov19data, selectedOption);
cout << "\n\nPercentage of recovered is: " << percentageRecovered(cov19data, selectedOption);
}
cout <<"\n\n";
} while (true);
}
void showElements(float cov19data[][4]) // function body implementation
{
cout << "Source Data:\n\n";
cout << "Country\tCases\tDeaths\tRecovered\n\n";
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
cout << cov19data[i][j];
cout << "\t";
}
cout << "\n";
}
}
double percentageDeath(float cov19data[][4], int selectedOption)
{
double deathPercentage = (cov19data[selectedOption][2] / cov19data[selectedOption][1]) * 100.0;
return deathPercentage;
}
double percentageRecovered(float cov19data[][4], int selectedOption)
{
double recoveredPercentage = (cov19data[selectedOption][3] / cov19data[selectedOption][1]) * 100.0;
return recoveredPercentage;
}
Comments
Post a Comment