C++ :: Calculate Average From File Containing Array Of Numbers?

Feb 21, 2014

I have written two separate programs; Program 1 calculates the average of an array of numbers which has been hard-coded into the program. Program 2 reads an array of numbers from a text file and displays them on the output.

I wish to combine these two programs into one by making Program 1 read the array of numbers from the file and then calculate the average using that instead of the array { 84, 92, 76, 81, 56 } as outlined below. I only wish to display the average in the output, not the number array as Program 2 does.

I have tried to do most of the work, I just need modifying the code slightly so it reads the number array from the file and calculates the average.

Program 1

#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <string>
#include <numeric>
using namespace std;
int main() {
const int nNumStudents = 5;

[Code] .....

View 7 Replies


ADVERTISEMENT

C/C++ :: Calculate Sum And Average Of A Sequence Of Numbers

Feb 17, 2014

Write a C program that calculates the sum and average of a sequence of numbers. Assume the first number specifies the number of values remaining to be entered. If the first number is less than 1, then the program should display an "Invalid Entry ... Please enter a positive number." message.

THIS IS HOW IT SHOULD COME OUT...
Enter the number of values to process 0

Invalid Entry ... Please enter a positive number.

Enter the number of values to process 3
Enter 3 numbers:
1
2
3

Sum: 6
Avg: 2

THIS IS THE CODE I HAVE SO FAR...

#include <stdio.h>
int main(void) {
int total=0;
int howmany;
int i;
int value;

[Code] ....

View 8 Replies View Related

C++ :: Program To Calculate Average Of 3 Numbers - Undeclared Identifier

Sep 22, 2013

Write a program that will calculate average of 3 numbers

#include <iostream>
int
main(){
// prototypes:
float add_and_div(float, float,
float);

[Code] .....

i get the following errors:

error C2065: 'cout' : undeclared identifier
error C2065: 'cin' : undeclared identifier
error C2065: 'cout' : undeclared identifier
error C2065: 'endl' : undeclared identifier

View 2 Replies View Related

C++ :: Calculate Average Of A Stream Of Positive Numbers - Loop Program?

Feb 25, 2013

Write a program that calculates the average of a stream of positive numbers. The user can enter as many positive numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, treat zero as a positive number, i.e., zero counts as a number that goes into the average. Of course, the negative number should not be part of the average. You must use a function to read in the numbers, keep track of the running sum and count, compute the average, and return the average to the main() function. Note that you must use a loop to do this, as you don't know how many numbers the user will enter ahead of time. I am having problem writing a loop program for it..

View 1 Replies View Related

C++ :: Calculate Average From Array Values

Jan 8, 2014

i wrote following code to calculate average of the values entered to the array.After displaying the output following error was displayed.

"Run-Time Check Failure #2 - Stack around the variable 'marks' was corrupted.

A buffer overrun has occurred in q 3 410005111.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program. "

//Average mark of student 1
cout<<"Avarage mark of the student 1 :"<<(marks[0][0]+marks[1][0]+marks[2][0]+marks[3][0]);
cout<<endl;

View 5 Replies View Related

C :: How To Calculate Largest / Smallest / Average - No Array

Feb 16, 2015

i'm having trouble trying to make this code works .. the program must repeatedly asks user for the age of a person until user enters -1 ,, once he/she entered -1 the program calculates ( smallest value entered, largest value , and average )excluding -1 from calculation

i've been working on this for a weeks this is the best i could do.i know average must add all the entered values , not only smallest and largest but how can i do that here ?

Code:

#include<stdio.h>
int main(void)
{
int age ;
int v;
int counter;
int l=0;
int s=0;
int limit;
}

[code]....

View 13 Replies View Related

C++ :: Calculate Average Of Array And Print Elements

Oct 23, 2013

Use function decomposition.

-To prompt user to enter value and assign it to correct position in array
-calculate the average of an array
-display all element.
-display average.

For some reason, i keep getting error and it doesn't compile. I use Dev-C++

Here is my code:

#include <iostream>
#include <iosmanip>
#include <math.h>
using namespace std;
const int SIZE = 5;
const int LINE = 4;
//function prototypes
void getData(int arr[]);

[Code]...

My error is on line 69. I don't see anything wrong with it.

If i take out setw(3), it doesnt get any error but for some reason it doesnt run. Possible i did something wrong there?

View 2 Replies View Related

C++ :: Using A For Loop To Input And Output Array Values Also Calculate The Average

Jan 24, 2014

I have this code that im stuck on what i need to do is Extend the code such that when it starts the user is asked to input values to specify each of the three ranges before moving on to accept and tally the main values how do i do that Using a for loop to input and output array values Also calculate the average

*/
#include <stdio.h>
int main(void)
{
/* Declare an array of integers */
int Grades[5];
int nCount;
int nTotal = 0; /* Declare and initialise the value */
float fAverage;

[Code]...

View 1 Replies View Related

C++ :: Using Array Of 5 Scores And Calculate Average / Highest And Lowest Score

Apr 11, 2013

it compiles and runs, it gives me the average and the highest score but says lowest is 0 even though 0 is never entered.

Write a program which uses an array of 5 scores and calculates the average score, the highest score and the lowest score.

#include <iostream>
using namespace std;

int getHighest(int [], int);
int getSmallest(int [], int);
double getAverage(int [], int);
int count;
int numbers;

[Code] ......

View 2 Replies View Related

C/C++ :: Get Average Of Numbers In Array

Jul 5, 2014

I was trying to make a program that asks the number of grades, get the grades and then get the average. I know I have to save the grades in an array but i don't know exactly how.

View 12 Replies View Related

C++ :: Average Negative / Positive Numbers And Total Average?

Nov 18, 2013

I am trying to average the negative numbers and positive number and of course the total average.

This will read in a list of n values, where n is not known ahead of time. The number of values read into the array will be saved in n.

vector<int> readList() {
std::vector<int> result;
ifstream inFile;
inFile.open("setA.txt");
for (int x; inFile >> x; ) {
result.push_back(x);

[code]....

array is a one-dimensional array of integers and n is the number of elements in that array that contain valid data values. Both of these are input parameters to the function. The function must calculate 1) the average of the n integers in array, storing the result in ave; 2) the average of the positive numbers (> 0), storing the result in avePos, and 3) the average of the negative numbers (< 0), storing the result in aveNeg.

void avgs (std::vector &array, int &ave, int &avePos, int &aveNeg) {
int sum = 0, pos_sum = 0, neg_sum = 0, pos_count = 0, neg_count = 0;
for (auto i : array) {
sum += i;
if (i > 0) { pos_sum += i; ++pos_count; }

[code]....

View 1 Replies View Related

C++ ::  How To Obtain Total Of Numbers Greater Than Average Without Using Array

Jul 27, 2014

I am facing a problem which i could not obtain the total numbers which is greater than the average value. For example:

#include <iostream>
using namespace std;
int main (){
int size , count;
double no, max, min ,total, sum , average;

[Code] ....

In this case im able to compute the average of the numbers but when it comes to capture the total of numbers which is greater than the average value, how to compile the code , because the average number is only been compute once all the value capture by the input of user is sum up.

View 19 Replies View Related

C++ :: Pass Array Of Int And Its Size - Print Out Numbers Above Average

Nov 15, 2013

I am attempting to write a program " that has a function that is passed an array of int and its size, and with that it prints out only the numbers that are above average. "

I have included my code so far, but I am only getting one value as output, and that value seems to be completely off. I keep trying, but I am really stuck.

#include <iostream>
using namespace std;
int average(int values[],int size);
int main(){
int size;
int values[] = {1,2,3,4,5,6};

[Code] ....

Added the floats in the average() function. But there is still a value problem.

View 1 Replies View Related

C/C++ :: Find Average Of 10 Numbers Passed To Array Using Function?

Jun 20, 2012

find the average of 10 numbers to an array using function .the array should be controlled by while loop?

#include <iostream.h>
float sum(float x[],int size);
    main() {
   float a[10];
int n=10;

[Code] .....

View 2 Replies View Related

Visual C++ :: Program That Reads Numbers From A File And Calculates Average

Mar 13, 2014

[URL] .....

Here is my code and basically these are the steps. I feel like we have something good to work on but we keep getting errors.

a. Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many numbers are in the file.

b. Save the output of the program in a file.

c. Modify the function getNumber so that it reads a number from the input file (opened in the function main), outputs the number to the output file (opened in the function main), and sends
the number read to the function main. Print only 10 numbers per line.

d. Have the program find the sum and average of the numbers.

e. Modify the function printResult so that it outputs the final results to the output file (opened in the function main). Other than outputting the appropriate counts, this new definition of the function printResult should also output the sum and average of the numbers.

View 12 Replies View Related

C++ :: Write A Source Code That Find Smallest / Largest And Average Of Numbers From Array

May 13, 2014

im trying to write a source code that find the smallest, largest and average of numbers in array. the code runs fine, but it is not giving the highest number and the the average should include only four number excluding highest and smallest number from the array.

void OlympicJudging() // Olympic Judging {
int numbers [6];
double average, sum = 0;
int temp;
for(int i = 0; i < 6; i++){
cout << "Please type a value for scores: ";
cin >> numbers[i];

[Code]...

View 5 Replies View Related

C/C++ :: Calculate Percentage Of Even Numbers Entered By User In Array

Nov 18, 2014

exercise in c: write a program which calculates % of even numbers entered by a user in an array;

1) check if user hasn't entered float value; if has issue warning;
2) give the option for user to press "stop" to exit program any time.

int c,sk[100],i,sum1=0, sum2=0, rel;
printf ("How many numbers will you enter?
");
scanf ("%d", &c);
printf ("Enter %d numbers (to exit program press - stop)
", c);

[Code] ....

View 1 Replies View Related

C++ :: How To Calculate Certain Cells Of Numbers Instead Of Entire Text File

May 24, 2014

As you can see, the code below calculates the average, sum, number of items, etc, for a list of numbers in two separate files and compares them with one another.

e.g. we will have a text file of 10 numbers;

45
65
24
26
26
36
35
100
109
433
etc...

The problem is that the code will perform calculations on all the numbers in the txt. or csv. file. This is problematic because if there is any text in the file, e.g. headings, then the calculations will not be performed. For instance, suppose that I only wanted to include rows 5-10 in the calculations, how would I specify this in my C++ code?

#include <iostream>
#include <cmath>
#include <math.h>

[Code]....

View 3 Replies View Related

C++ :: Random Number Generator And Calculate Average

Oct 15, 2014

I need to create a program that has has an array size of 20 and will generate 20 random numbers in it between 1-1000. Then I need to calculate the average of all the random numbers.

I have half of the code done ,but im lost on how to calculate the average of the random numbers.

Here what I have:

#include <iostream>
using namespace std;
int i;
int size[20];
int counter = 0;
int main() {
cout << "The Array Consists Of 20 Random Numbers: "<<endl<<endl;

[Code] ....

View 2 Replies View Related

Visual C++ :: How To Calculate Average And Standard Deviation

Oct 21, 2013

I've been trying to calculate the Second standard deviation but the average in the second loop isn't calculating correctly which is causing the standard deviation (method 2) to not calculate correctly.

Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
int main () {
cout << "
This program will produce statistics (Mean, Standard Deviation, "

[code]...

View 4 Replies View Related

C++ :: Test Scores Program With Pointers - Calculate Average?

Apr 7, 2013

I have created a program that allows for a user-defined number of test scores as input. After the user enters these scores, the program calculates the average test score. Here is my code:

#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
double getAverage(double*, int);

[Code] .....

I am having trouble with the final part of my program. How do I pass the array of test scores to a function that calculates how many people got an A (90+) on the test? The final output should look like this:

The average of those scores is:
The number of A grades is:

View 2 Replies View Related

C++ :: Read List Of Random Numbers From Input File And Calculate Statistics?

Nov 11, 2013

I need my program to read a list of numbers from and input file, random.txt, and calculate the following statistics on those numbers:

A. The number of numbers in the file.
B. The sum of all the numbers in the file.
C. The average of all the numbers in the file.
D. The largest number in the file.
E. The smallest number in the file.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

[Code]......

This is what I have so far but I'm not sure if it is right. I can't get the input file to open.

View 3 Replies View Related

C/C++ :: Find Average Of 20 Students By Using A File And One Dimensional Array

Jan 22, 2015

is this according to it??

#include <iostream>
#include <fstream>
using namespace std;
int main(void){
fstream file("textfile.txt");
int linecount = 0;
int tempArray[100];

[Code] ....

View 1 Replies View Related

C++ :: Dynamic Arrays - Sort Input Data And Calculate Average

Jul 15, 2014

I'm still fairly new to c++ and am having trouble with dynamic arrays. My task is to create an array for test scores. The user needs to define the size of the array and then input data to later sort and calculate the average.

Below is what I have so far. I am struggling with the user input part. I receive the following errors for the data input of the individual scores:

"error C2108:subscript is not of integral type"
"IntelliSense:expression must have integral or unscoped enum type"

Code :

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
//user input total number of scores
cout<<"Please enter the total number of test scores."<<endl<<endl;

[Code] ....

View 1 Replies View Related

C++ :: Count The Numbers Below The Average

Mar 7, 2013

I have loaded and 1d array from a .dat file, calculated the average of all the numbers. Now I need to count every number in the array that is below the average and cout that number. This is the method in the class I have so far:

int IntegerArray::countBelowAverage(int numBelowAvg) {
avg=IntegerArray::getAvg();
for(int ct=0; ct<avg; ++ct) {
numBelowAvg=ct;
}
return numBelowAvg;
}

My output from this is always 0 and I know that there are numbers below the avg.

View 2 Replies View Related

C :: Find Average Of Random Numbers

Aug 17, 2013

I have the random values down but when I try to assign randValue to another integer I get an error.I want to make an inner loop where I find 5 random number 50 times and print the average of those numbers.

I know I have the bare bones of the below, but without giving me a huge shove in the right direction can I have a poke? I have been programing for three months now. I have noticed when I do simple fun projects I learn more then what my professor assigned to me. The class is over but I want to keep building.C++ starts in one week...

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBERS 5

int main(int argc, const char * argv[]) {
float randValue = 0.0;
int i;

[code]....

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved