Server IP : 172.16.15.8 / Your IP : 3.145.10.68 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/drsparks/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// CS 112 Computer Programming 1 // Assignment 7 // File: assign7.cpp // Author: Heather Phelps // Instructor: Dr. Wang // Due: Mon. Oct 26, 2009 // Compile: g++ assign7.cpp // Run: ./a.out < data.txt // Goal: This program will read a file with unknown # of int, // calculate the avg for the positives and negatives for each line, // and the total amount, and output the results. // #include <iostream> using namespace std; int main() { int data; int num_pos; int sum_pos; int sum_neg; int num_neg; int line_num = 0; int total_num_pos = 0; int total_sum_pos = 0; int total_num_neg = 0; int total_sum_neg = 0; float total_pos_avg; float total_neg_avg; cin >> data; while(cin) { // process each line line_num ++; num_pos = 0; sum_pos = 0; num_neg = 0; sum_neg = 0; while( data != -1) { if( data > 0 ) { sum_pos += data; num_pos ++; } else if ( data < 0 ) { sum_neg += data; num_neg ++; } cin >> data; } cout << "Line #: " << line_num << endl; cout << "# of pos.: " << num_pos << endl; cout << "Sum of pos.: " << sum_pos << endl; cout << "Average of pos.: " << 1.0*sum_pos/num_pos << "\n"; cout << "# of neg: " << num_neg << endl; cout << "Sum of neg: " << sum_neg << endl; cout << "Average of neg: " << 1.0*sum_neg/num_neg << endl; total_num_pos += num_pos; total_sum_pos += sum_pos; total_num_neg += num_pos; total_sum_neg += sum_neg; cin >> data; } total_pos_avg = 1.0*total_sum_pos/total_num_pos; total_neg_avg = 1.0*total_sum_neg/total_num_neg; cout << "The avg of all pos. is: " << total_pos_avg << endl; cout << "The avg of all neg. is: " << total_neg_avg << endl; return 0; }