Write a C++ program that generates 7 random numbers indicating the number of cars crossing a bridge each day (1 random number is assigned for each day [Monday-Sunday]), calculates the daily average number of cars crossing the bridge, minimum and maximum number of cars crossing the bridge in a day and sort the number of cars in descending order. The program should display a menu with the following options.
1.Generate 7 Random Numbers %26amp; Store in an Array
2.Calculate Average Number of Cars Crossing the Bridge
3.Calculate Minimum and Maximum Number of Cars in a Day
4.Sort the Number of Cars Crossing the Bridge in Descending Order
5.Exit
Ex:Assume that the menu option a) generated the following numbers.
33 10 97 89 20 1 67
Option d) should display
97 89 67 33 20 10 1
Wed Thurs Sun Mon Fri Tue Sat
The whole program should work in a loop to enable the user to generate numbers, calculate average, minimum and maximum number oof cars in a day, and sort the numbers untilthe user chooses Exit
Write a C++ program that generates 7 random numbers indicating the number of cars crossing a bridge each day (
Here u go mate...I hope I deserve 10 pts. :)
/*
Write a C++ program that generates 7 random numbers indicating the number of cars
crossing a bridge each day (1 random number is assigned for each day [Monday-Sunday]),
calculates the daily average number of cars crossing the bridge, minimum and
maximum number of cars crossing the bridge in a day and sort the number of cars
in descending order. The program should display a menu with the following options.
1.Generate 7 Random Numbers %26amp; Store in an Array
2.Calculate Average Number of Cars Crossing the Bridge
3.Calculate Minimum and Maximum Number of Cars in a Day
4.Sort the Number of Cars Crossing the Bridge in Descending Order
5.Exit
Ex:Assume that the menu option a) generated the following numbers.
33 10 97 89 20 1 67
Option d) should display
97 89 67 33 20 10 1
Wed Thurs Sun Mon Fri Tue Sat
The whole program should work in a loop to enable the user to generate numbers,
calculate average, minimum and maximum number oof cars in a day, and sort the
numbers untilthe user chooses Exit
*/
#include "stdafx.h"
#include %26lt;time.h%26gt;
#include %26lt;iostream%26gt;
using namespace std;
#define MAX_DAYS7
void usage(){
printf("\n\n1.Generate 7 Random Numbers %26amp; Store in an Array.\n");
printf("2.Calculate Average Number of Cars Crossing the Bridge.\n");
printf("3.Calculate Minimum and Maximum Number of Cars in a Day.\n");
printf("4.Sort the Number of Cars Crossing the Bridge in Descending Order.\n");
printf("5.Exit.\n\n");
}
typedef struct __cars{
int m_iCarCount;
char m_cDays[10];
}CARS;
class CCarCrossing{
CARS m_cars[MAX_DAYS];
CARS m_carsSorted[MAX_DAYS];
public:
CCarCrossing(){
for(int i = 0; i %26lt; MAX_DAYS; i++)
m_cars[i].m_iCarCount = 0;
strcpy(m_cars[0].m_cDays, "MON");
strcpy(m_cars[1].m_cDays, "TUE");
strcpy(m_cars[2].m_cDays, "WED");
strcpy(m_cars[3].m_cDays, "THU");
strcpy(m_cars[4].m_cDays, "FRI");
strcpy(m_cars[5].m_cDays, "SAT");
strcpy(m_cars[6].m_cDays, "SUN");
}
void GenerateCarCount(){
srand( (unsigned)time( NULL ) );
for(int i = 0; i %26lt; MAX_DAYS; i++)
m_cars[i].m_iCarCount = rand() % 100;
}
void DisplayCarCount(){
int i = 0;
for(; i %26lt; MAX_DAYS; i++)
printf("%s\t", m_cars[i].m_cDays);
printf("\n");
for(i = 0; i %26lt; MAX_DAYS; i++)
printf("%d\t", m_cars[i].m_iCarCount);
printf("\n");
}
void DisplaySortedCarCount(){
int i = 0;
for(; i %26lt; MAX_DAYS; i++)
printf("%s\t", m_carsSorted[i].m_cDays);
printf("\n");
for(i = 0; i %26lt; MAX_DAYS; i++)
printf("%d\t", m_carsSorted[i].m_iCarCount);
printf("\n");
}
void DisplayAverage(){
int iTotal = 0;
for(int i = 0; i %26lt; MAX_DAYS; i++)
iTotal += m_cars[i].m_iCarCount;
printf("Average Number of Cars Crossing the Bridge = %d\n", iTotal / MAX_DAYS);
}
void DisplayMaxMin(){
int iMax = m_cars[0].m_iCarCount;
int iMin = m_cars[0].m_iCarCount;
char cMaxDay[10];
char cMinDay[10];
strcpy(cMaxDay, m_cars[0].m_cDays);
strcpy(cMinDay, m_cars[0].m_cDays);
int i = 1;
for(; i %26lt; MAX_DAYS; i++)
(m_cars[i].m_iCarCount %26gt; iMax) ? iMax = m_cars[i].m_iCarCount, strcpy(cMaxDay, m_cars[i].m_cDays) : 0;
for(i = 1; i %26lt; MAX_DAYS; i++)
(m_cars[i].m_iCarCount %26lt; iMin) ? iMin = m_cars[i].m_iCarCount, strcpy(cMinDay, m_cars[i].m_cDays) : 0;
printf("Max Number of cars were on %s. The number of cars were %d.\n", cMaxDay, iMax);
printf("Min Number of cars were on %s. The number of cars were %d.\n", cMinDay, iMin);
}
void Sort(){
for(int k = 0; k %26lt; MAX_DAYS; k++)
m_carsSorted[k] = m_cars[k];
// using insertion sort
int i, j;
for(j = 1; j %26lt; MAX_DAYS; j++){
CARS key = m_carsSorted[j];
i = j - 1;
for(; (i %26gt;= 0) %26amp;%26amp; (m_carsSorted[i].m_iCarCount %26lt; key.m_iCarCount); i--)
m_carsSorted[i + 1] = m_carsSorted[i];
m_carsSorted[i + 1] = key;
}
}
CARS maxCar(int low){
CARS max = m_cars[low];
for(int i = low;i %26lt; MAX_DAYS; i++)
if(max.m_iCarCount %26lt; m_cars[i].m_iCarCount)
max = m_cars[i];
return max;
}
~CCarCrossing(){}
};
int main(int argc, char* argv[]){
int iInput = 0;
//for(int i = 0; i %26lt; 10; i++){
//int j = rand() % 100;
//printf("%d\n", j);
//}
CCarCrossing cars;
for(; iInput != 5;){
usage();
scanf("%d", %26amp;iInput);
switch(iInput){
case 1:
cars.GenerateCarCount();
cars.DisplayCarCount();
break;
case 2:
cars.DisplayAverage();
break;
case 3:
cars.DisplayMaxMin();
break;
case 4:
cars.Sort();
cars.DisplaySortedCarCount();
break;
case 5:
exit(0);
break;
default:
printf("opt NULL\n");
}
}
return 0;
}
Reply:There's not enough space here to write the whole thing out, but I can give you some pointers.
Fill an array with random numbers:
// Need this for the rand() function
#include %26lt;stdlib.h%26gt;
const int cNumValuesToMake=7; // 7 days in a week
const int cMaxNumber=100; // 1-100 cars per day
// Array to store the values
int myArray[cNumValuesToMake];
for (int i=0; i%26lt;cNumValuesToMake; i++)
{
myArray[i]=(rand()%cMaxNumber)+1;
}
You can sort that array using a bubble sort, as so:
for (int i=cNumValuesToMake-1; i%26gt;0; i--)
{
for (int j=0; j%26lt;i; j++)
{
if (myArray[j]%26lt;myArray[j+1])
{
int temp=myArray[j];
myArray[j]=myArray[j+1];
myArray[j+1]=temp;
}
}
}
And finally figure out the mean, min and max as so:
int min=cMaxNumber;
int max=0;
float mean;
for (int i=0; i%26lt;cNumValuesToMake; i++)
{
if (myArray[i]%26gt;max)
max=myArray[i];
if (myArray[i]%26lt;min)
min=myArray[i];
mean+=(float)myArray[i];
}
mean/=(float)cNumValuesToMake;
Hope this helps :)
yu gi oh cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment