Question

Write a C++ program that do : Suppose we have three cities city1,city2,city3 The information of...

Write a C++ program that do :

Suppose we have three cities
city1,city2,city3
The information of each city will be stored in one row in 2 D array of integers
Suppose you want to store the number of accidents that happens in each week on July.
The program should read the values of each week for each city from KB.
Ex. Enter city1 information
Week 1: …
Week 2: …
Week 3: …
Week 4: …
Enter city2 information
Week 1: …
…..
and so on
Use for loops for entering values. Use the same for loops for entering values for the whole cities. (you are now allowed to make separate loop for each city )

The program should find which week has the largest number of accidents. ( write a function for this and call it)

Important Note :using pointers .

Homework Answers

Answer #1
  • Inorder to get the inputs for three cities, write a for loop that iterates three times
  • And to get data for 4 weeks for each of these cities, write a for loop inside the previous loop that iterates 4 times.
  • To store these elements in a 2D array, define a array of size 3x4. It is better to dynamically allocate these arrays just before reading in the data
  • we will use a function calcMax() to calculate the city with maximum number of accidents.
  • this function will take in the address of 2D array.
  • to access the row and columns of this array, we will be adding the offset to the base address of the array
  • ith row of our array can be accessed by using *(array + i). jth column in the ith row can be accesed by *(*(array + i) + j)

program :

#include <iostream>

using namespace std;

void calcMax(int** accidentData){
int max = 0;
int maxCount = 0;
for(int i = 0; i<3;i++){
int count = 0;
for(int j = 0; j<4; j++){
count+= *(*(accidentData+i)+j);
}
if(count>maxCount){
max = i;
maxCount = count;
}
}
cout<<"city "<<max+1<<" has most number of accidents";
cout<<" with a tolal of "<<maxCount<<" accidents in july\n";
}

int main(){
int* accidentData[3];
for(int i = 0; i<3; i++){
accidentData[i] = new int[4];
cout<<"Enter city"<<i+1<<" information\n";
for(int j = 0; j<4; j++){
cout<<"Week "<<j+1<<" "; cin>>accidentData[i][j];
}
}
calcMax(accidentData);
return 0;
}

output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program which: Write a program which uses the following arrays: empID: An array of...
Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to hold each employee’s gross salary....
C++ The problem is as below: The Greatest and Least of These Write a program with...
C++ The problem is as below: The Greatest and Least of These Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered.
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Write a simple ARM assembly language program that finds the mean (as an integer) of all...
Write a simple ARM assembly language program that finds the mean (as an integer) of all values of an integer array. When your program starts, you should assume that r0 contains the address of the array and r1 contains the number of integers in the array. When you program finishes, the mean should be stored in r2. You may use other registers as stack registers.
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
Q :     Write a C++ program that asks the user to enter three integers and...
Q :     Write a C++ program that asks the user to enter three integers and then displays the largest one. Example : if the user enter ( 7, 2 ,5) the largest number will be 7 Or if user enter ( 2 , 5 , 7 ) the largest number will be 7 Or if user enter ( 7, 5 ,2) the largest number will be 7 In general it does not matter the order of the entered numbers...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT