Reading and looping (assignment)

In summary, this conversation discusses a difficult assignment involving processing data from sensors installed on a pipe carrying crude oil. The task is to create a menu-driven C++ program that allows the user to analyze the sensor data, including finding the maximum and average pressure, as well as the percentage of readings above the overall average. The program should also be able to generate a summary report to an output file. The conversation also mentions difficulties with creating a read function and handling user input.
  • #1
Lord Dark
121
0

Homework Statement



Hi everyone ,, I've got the following assignment and it's very hard for me ,, I couldn't understand half of it ,, that's if it's not all -_-

The assignment is :

Consider a set of sensors installed at different locations of pipe carrying crude oil. These sensors periodically (say every 10 minutes) measures the pressure inside the pipe and record that value in a text file. Your task is to write a menu driven C++ program that will allow the user to process the data files generated by these sensors. Example of sensor data files are shown in Figure 1. The program should provide the user with the following options:
1. Read sensor data.
2. Find maximum pressure of a given sensor
3. Find average pressure of a given sensor
4. Find percentage of readings of a given sensor that are above the overall average
5. Find overall maximum reading and its sensor number.
6. Find overall average pressure
7. Write a summary to a text file
8. Exit the program


Please note the following:
• The maximum number of sensors is 10 and each sensor file can contain at most 100 readings.
• You are not allowed to declare any global variables. Constants however can be global.
• Only one array (data array) of double values should be used to store data from all sensor files. Another array (sensors array) of integers should be used to store the sensor number for each corresponding reading. Figure 2 shows both arrays after reading data from three input files.

Your program should use the following functions:
• menu (. . . ): this function displays the above menu and returns the user choice.

• readSensorData (. . . ): this function receives both arrays, a sensor number and the array size. It should then read the data from an input file whose name is provided by the user to the data array. The sensors array should also be modified to reflect the changes (as illustrated in Figure 2). The function should also check the file, display an error message if it could not open it and return false. Otherwise, it returns true. The function should also return the new array size through the size parameter.

• findMaxIndex ( . . . ): this function receives both data and sensors arrays, a sensor number and the array size. It returns the index of the maximum pressure reading for the given sensor number.

• findAverage ( . . . ): this function receives both data and sensors arrays, a sensor number and the array size. It returns the average pressure for the given sensor number.



FindPercentage ( . . . ): this function receives both data and sensors arrays, a sensor number and the array size and returns the percentage of the pressure values from the given sensor that are above the overall average pressure.

• findOverallAverage ( . . .): this function receives the data array and the array size. It then returns the overall average of all pressure readings in the data array.

• findOverallMaxIndex ( . . .): this function receives the data array and the array size. It then returns the index of the maximum pressure value in the data array.

• generateReport (. . . ): this function receives both data and sensors arrays and the array size and prints a summary report to an output file whom name is provided by the user. The function also displays a message on the screen indicating that the file has been written correctly or otherwise there have been an error while writing. Figure 3 shows a sample summary report.






Homework Equations





The Attempt at a Solution



OK ,, am at the first step which is making a read function ,, but my problem is that when I let it read a sensor text more than once it tell me to press any key to continue but I want it to continue until the user enters 10 sensors ... and the other problem is when the user press other than numbers the program goes up and down (due to while thing) and I don't know how to fix it ,, help please

I need a code for while so when I press a word like (w) it won't repeat by at self at infinity
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std ;

const int s_max = 100 ;
const int r_max = 100 ;


int menu(int choose) ;
void readSensorData(double r[],int s[],int &snum,int &count) ;


int main ()

{
static int snum(1) ;
double r[r_max] ;
int s[s_max] ;

int read(0), choice(0),c(0),count(0);



do {



choice = menu(c) ;


if ( choice < 0 && choice > 8)
choice = menu(c) ;

else
{
switch (choice)
{
case 1:
readSensorData(r,s,snum,count) ;
break;
case 8:
return 0 ;
}
} (while (what should I write here ??))

}
 

Attachments

  • soft.jpg
    soft.jpg
    64.3 KB · Views: 407
Last edited:
Physics news on Phys.org
  • #2
return 0 ;}int menu (int c) { cout << "\nPlease enter your choice:" ; cout << "\n1: Read sensor data" ; cout << "\n2: Find maximum pressure of a given sensor" ; cout << "\n3: Find average pressure of a given sensor" ; cout << "\n4: Find percentage of readings of a given sensor that are above the overall average" ; cout << "\n5: Find overall maximum reading and its sensor number" ; cout << "\n6: Find overall average pressure" ; cout << "\n7: Write a summary to a text file" ; cout << "\n8: Exit the program" ; cin >> c ; return c ;}void readSensorData(double r[],int s[],int &snum,int &count){ string fileName; ifstream inFile; int reading ; double value ; cout << "Enter the name of the file: " ; cin >> fileName ; inFile.open(fileName.c_str()) ; if (inFile.fail()) { cerr << "Error opening file" ; system("pause") ; exit(1) ; } while (!inFile.eof()) { inFile >> reading >> value ; r[count] = value ; s[count] = snum ; count++ ; } snum++ ; inFile.close() ; system("pause") ;}
 
  • #3
while (choice != 8) ;


}

int menu(int choose)

{
int c ;
cout << endl << "please enter the number of the task you want to do" << endl ;
cout << "1. Read sensor data" << endl ;
cout << "2. Find maximum pressure of a given sensor" << endl ;
cout << "3. Find average pressure of a given sensor" << endl ;
cout << "4. Find percentage of readings of a given sensor that are above the overall average" << endl ;
cout << "5. Find overall maximum reading and its sensor number" << endl ;
cout << "6. Find overall average pressure" << endl ;
cout << "7. Write a summary to a text file" << endl ;
cout << "8. Exit the program" << endl ;
cout << endl << "your choice : " ;
cin >> c ;
return c ;
}

void readSensorData(double r[],int s[],int &snum,int &count)

{
string name ;
int sensor ;
double reading ;
int scount ;
cout << "please enter the name of the file you want to read from : " ;
cin >> name ;
ifstream fin(name.c_str()) ;
if (fin.fail())
{
cout << "Error opening the file" << endl ;
}
else
{
fin >> sensor ;
fin >> reading ;
while (!fin.eof())
{
s[snum] = sensor ;
r[count] = reading ;
count++ ;
snum++ ;
fin >> sensor ;
fin >> reading ;
}
cout << endl << "file read successfully" << endl ;
}
fin.close() ;
}

Dear student,

It seems like you are struggling with some aspects of the assignment, particularly with the looping and handling user input. First of all, it is important to understand the purpose of the assignment and the functions provided. This assignment is asking you to create a program that will allow a user to process data from multiple sensor files and perform various calculations on the data.

In order to achieve this, you will need to use loops to read the data from each sensor file and store it in the appropriate arrays. The while loop in your code is a good start, but it is currently only reading one sensor file. To read multiple files, you will need to use a for loop that iterates through each sensor file and calls the readSensorData function
 

1. What is reading and looping in programming?

Reading and looping in programming refers to the actions of reading and processing data from a source, such as a file or user input, and then repeating a set of instructions for each data item until a specified condition is met.

2. Why is reading and looping important in programming?

Reading and looping is important in programming because it allows for efficient processing of large amounts of data and repetitive tasks. It also enables the creation of dynamic and interactive programs that can respond to user input.

3. What are some common methods for reading and looping in programming?

Some common methods for reading and looping in programming include using a while loop, for loop, and do-while loop. Other methods include using arrays, iterators, and recursion.

4. How does reading and looping differ from other programming concepts?

Reading and looping is a fundamental programming concept that is used in many different contexts, such as data analysis, web development, and game development. It differs from other programming concepts, such as conditional statements and functions, in that it specifically focuses on processing and repeating data.

5. What are some tips for effectively using reading and looping in programming?

Some tips for effectively using reading and looping in programming include understanding the data being processed, choosing an appropriate loop structure, and using break and continue statements to control the flow of the loop. It is also important to consider efficiency and potential edge cases when implementing a loop.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top