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;

[Code] .....

View 12 Replies


ADVERTISEMENT

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++ :: 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 :: Flowchart - Program To Compute Average Of N Numbers

Feb 20, 2015

How can you draw a flow chart that will be used to write a program to compute Average of n numbers?

View 1 Replies View Related

C++ :: Program That Compute Final Average Grades Of Class

Jan 28, 2013

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:

Exam 1 – 30%
Exam 2 – 30% Quiz 1 – 10%
Quiz 2 – 10%
Quiz 3 – 10%
Quiz 4 – 10%

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.

View 1 Replies View Related

C++ :: Compute Average Median And Mode For Numbers In Vector?

Feb 23, 2014

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.

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

[Code]....

View 7 Replies View Related

C :: Function For Raising Numbers To Positive Integers

Sep 18, 2013

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).

View 2 Replies View Related

C :: Entering Two Positive Integers To Multiply Together - Program Won't Compile

Aug 24, 2013

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"

View 11 Replies View Related

C :: Finding Positive / Negative Integers Unsigned Can Hold

Jan 25, 2013

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?

Not sure how to determine this. I'm stuck.

View 5 Replies View Related

C++ :: Reading Integers Out Of A Text File And Only Adds Positive Ones?

Sep 26, 2014

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;

[Code] .....

Here's the txt file:
5
6
-9
21
3

I'm always getting 32 for some reason.

View 2 Replies View Related

C++ :: Reads In Two Positive Integers That Are 20 Or Fewer Digits In Length - Output Sum?

May 1, 2013

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

[Code] .....

View 1 Replies View Related

C++ :: Create A File And Fill 6x6 Array With Random Positive Integers

Nov 24, 2014

This is my code!! but it's not working at all.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
ofstream fout("datain.txt",ios::out);
int array[6][6];

[Code] .....

View 5 Replies View Related

Visual C++ :: Loop To Compute Both Max And Min Of Vector?

Jul 12, 2014

How would I make a loop that would simultaneously compute both the max and min of a vector?

View 2 Replies View Related

C++ :: Read Two Positive Integers That Are 20 Or Fewer Digits In Length And Output Sum Of Two Numbers

Apr 30, 2013

so basically my project goes like this:

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);

[Code]...

View 2 Replies View Related

C :: Average And Total Of Six Integers

Jan 22, 2013

"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;

printf("Enter first number:");
scanf("%2d",&num);

[Code] .....

View 2 Replies View Related

C++ :: Will Overflows Loop Back Around And Become Positive Again?

Apr 24, 2012

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?

View 4 Replies View Related

C/C++ :: How To Find Average With For Loop

Sep 14, 2014

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;

[Code] ....

View 2 Replies View Related

C/C++ :: Do While Loop And Average Calculation

Dec 9, 2014

I need to create average calculating program using do while loop.

t = 1 - sin(x) when x > 0;
t = 1 + x when x = 0;
t = a + sin(x) when x < 0;
primary information:
x changes from 1 to -1 by step h = -0.5, a = -2

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main() {
int a = -2;

[Code] ....

How this program should look that's why this probably looks just like a mess.

View 2 Replies View Related

C :: Finding Largest / Smallest / Average In A Loop

Feb 9, 2013

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
}

[code].....

View 4 Replies View Related

C/C++ :: Using Program That Calculates Test Average Using While Loop?

Sep 23, 2014

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?

View 1 Replies View Related

C/C++ :: Finding Average Time Complexity Of A Nested Loop?

Oct 29, 2014

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.

View 13 Replies View Related

C++ :: Finding Smallest / Largest And Average Number - Do While Loop

Sep 10, 2013

I am stuck looking at whats the problem ....

#include <iostream>
using namespace std;
int main() {
float average, largest = 0, smallest = 0;
int count = 1;
int num;

[Code] .....

View 1 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++ :: STL - Loop To Access Integers

Sep 17, 2013

I have to write a loop to access the integers. Then fill it with few sample data.

list<map<set<string>,int> l;

View 15 Replies View Related

C++ :: Terminate Loop When Two Integers Between 1 To 100 Are Equal

Mar 21, 2013

creating a program using while that terminates the loop when two intergers any two between 1 to 100 are equal with each other

View 2 Replies View Related

C/C++ :: Program That Uses While Loop To Read A Set Of Integers From Input File?

Apr 20, 2014

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";

[Code]...

View 5 Replies View Related







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