C/C++ :: Compute Sum And Average Of All Positive Integers Using Loop
Oct 8, 2014
I got an assignment which asked me to create a program that asks user to input 10 numbers (positive and negative) integer. The program would be using loop to compute sum and average of all positive integers, the sum and average of negative numbers and the sum and average of all the number entered. But, the new problem is after I've entered all the number, the answer that the program gave me wasn't the answer that the program supposed to give. I don't know how to describe it, I would attach a picture and the code below:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//Declaration
int x, value, sum, average, SumPositive, SumNegative, Positive, Negative;
float AveragePositive, AverageNegative;
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..
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; }
An instructor needs you to write a program that will compute the final average grades in her class. There are 5 students in the class. Each student must take 2 exams and 4 quizzes as part of their final grade. The weight of each toward the final grade is as follows:
You must use arrays to store each students 3 digit ID number, exam scores and quiz scores. A multi-dimensional array is mandatory. Request from the user the information needed then output all of the information as well as the final average grade for each student.
I needed to compute average median and mode for numbers in a vector. Im having a few issues I have already figured out how to calculate average and median just not mode. Also the program crashes when 0 is entered instead of exiting nicely. Also i keep getting a error on line 47 saying primary expression expected before else.
Write a function that raises an integer to a positive integer power. Call the function x_to_the_n taking two integer arguments x and n. Have the function return a long int, which represents the results of calculating x^n.Here's my code:
Code:
#include <stdio.h> long int x_to_the_n(int x, int n) { int i; long int acc = 1;
for(i = 1; i <= n; ++i) acc *= x; }
[code]...
It compiles OK, but when I run it the program stops after entering the number (x) and power (n).
I'm trying to get my C program to compile but it's not working at all. I've programmed a little in C++ before but I'm not used to C. Here's my program so far:
Code: int main(void){ // Establishes variables int num1, num2, product; float quotient;
[Code] .....
It keeps giving me an error message as follows:
"/usr/bin/ldrelabwk2: file format not recognized; treating as linker script /usr/bin/ldrelabwk2:1: syntax error collect2: ld returned 1 exit status"
Consider a new data type, the mikesint, which can hold 9 bits.
(a) What is the largest integer that an unsigned mikesint can hold? (b) What is the largest positive integer that a signed mikesint can hold? (c) What is the largest negative integer that a signed mikesint can hold?
I'm basically trying to make a simple program that reads integers out of a text file and only adds the positive ones and not the negatives.
All is well except it won't take the last integer (the last line, I presume.) I took the negative out, nothing to do with that. I put more numbers in and I made the txt file less, no answer. No matter what the last number is, the program won't read it. I've been researching online and I've been seeing that it might be an issue with "while "!inFile.eof())".
Anyway here's the program:
#include <iostream> #include <fstream> using namespace std; int main() { ifstream inFile; ofstream outFile;
Write a C++ program that reads in two positive integers that are 20 or fewer digits in length and outputs the sum of the two numbers.
Your program will read the digits as values of type char so that the number 1234 is read as four characters '1', '2', '3' and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array and you might find it useful to reverse the order of the elements in the array after array is filled with data from the keyboard.
Your program will perform the addition by implementing the usual pencil and paper addition algorithm. The result of the addition is stored in an array of size 20 and the result is written to screen. if the result of the addition is an integer with more than maximum number of digits(that is more than 20 digits) then your program should issue a message saying that it has encountered "integer overflow".
You should be able to change the maximum length of the integers by changing only one globally defined constant. Include the loop that allows the user to continue to do more additions until the user says the program should end.
For some reason the sum won't add or output though, This is what i have so far:
#include <iostream> using namespace std; const int MAXIMUM_DIGITS = 20; void input_Large_Int (int a[], int& size_of_A); //input function for the two big integers void output_Large_Int(int a[], int size_of_A); //output function for the two big integers and the sum integer void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int & size_Sum); //add function for the big integers' sum
#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; int main () { ofstream fout("datain.txt",ios::out); int array[6][6];
Write a C++ program that reads in two positive integers that are 20 or fewer digits in length and outputs the sum of the two numbers.
Your program will read the digits as values of type char so that the number 1234 is read as four characters '1', '2', '3' and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array and you might find it useful to reverse the order of the elements in the array after array is filled with data from the keyboard.
Your program will perform the addition by implementing the usual pencil and paper addition algorithm. The result of the addition is stored in an array of size 20 and the result is written to screen. if the result of the addition is an integer with more than maximum number of digits(that is more than 20 digits) then your program should issue a message saying that it has encountered "integer overflow".
You should be able to change the maximum length of the integers by changing only one globally defined constant. Include the loop that allows the user to continue to do more additions until the user says the program should end. What I have so far is
#include <iostream> #include <cstdlib> using namespace std; void reverseArr(int a[], int liu); void addLargeInt(int a1[], int liu1, int a2[], int liu2, int sum[], int& liu_sum); int main() { cin.get(next);
"Write a program that prompts the user for an integer number from the keyboard, and store it in a variable num. After each number is typed in, add num to a total.
Do this six times, each time adding num to a total. (Be sure to reuse the same variable named num for each of the six numbers entered.) When all six numbers have been entered from the keyboard, calculate an average. Display the total and average calculated. "
Here is what I have so far:
Code: #include<stdio.h> int main() { int num, total1, total2, total3, total4, total5, total6, avg;
If I have a positive double and cast it to an int. If it overflows, I know that most of the time, the value of the int becomes negative. If it overflows far enough, will it eventually become positive again?
Code: double d = 34192384732194872394837249832743984738.; int i = (int)d; std::cout << i << std::endl;
Is there any value of d > INT_MAX that will cause i to be positive?
What i need it to do is ask the user for a number of cases. The user will input numbers and the program should add the inputs until zero or a negative number is entered and then out put the average of those inputs. The amount of cases is pretty much how many times an average will be done. so if the amount of cases is 4. and the inputs are 1,3,(1+3/2)0 then it should output 2. and that would be ONE case, next inputs are 5,6,4,0(5+6+4/3) the output is 5 and that is case two. etc.
Here is my code:
#include <iostream> using namespace std; double avgVal(int, int); int main() { int amountOfCases; cin >> amountOfCases; int* numbers = new int[amountOfCases]; int input=0;
I'm trying to write a program that will allow a user to enter infinite numbers (one at a time) with a way to stop entering values, and once it stops it immediately displays:
Lowest number: Highest number: Number of values entered: Average of numbers:
what I can't figure out is:
-how to end the loop with something other than a number, preferably a char, so that it doesn't affect the average/lowest/highest
-how to output the lowest and highest numbers in a loop (would be easier to figure out were it finite and I could just type a bunch of printf/scanf)
What I have so far:
Code:
#include <stdio.h>#include <stdlib.h> #define pause system("pause") main(){ //n = amount of numbers input //x = number given by user //s = smallest number //l = largest number }
I am working on a program that calculates the average of a list of test scores entered, with the list ended with a negative score (which is not calculated), as long as a user enters a name.
#include<iostream> #include<string> using namespace std; int main() { string name; double score = 0.0; //user input score
[code]....
I have gotten the while loop to function. The problem lies in the calculation in the average of the test scores, such as when I enter the scores 87 76 90 -1 , which is supposed to give an average of 84.3 but my result when outputted is 86. what the problem is and what I can do to fix this error?
We were discussing how to find average time complexity of different algorithms. Suppose I've a the following nested loop
for(int i=0;i<n;i++) { min = i;
[Code].....
Now the outer loop will iterate N times. the inner loop will always iterate 3 times no matter what the value of N is. so the worst case time complexity should be O(n^2) but what about the average case and the best case? I mean in terms of searching we can figure out and distinguish between the worst,best and average as it depends upon the number of comparisons. But here how can the average and best case be different then the worst case.
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;
The question is "Write a program that uses a while loop to read a set of integers from an input file called "data.txt" and prints the total number of even numbers read from the file."
Code below is the program I have created so far. I keep getting the window to pop up but not stay up so I am assuming I am doing something wrong.
#include <iostream> #include <fstream> using namespace std; int main() { std::fstream input("data.txt"); if (!input.is_open()) { std::cout << "There was an error opening data.txt";