Hello I wrote a c++ program to calculate Fibonacci numbers.
Basically I want my program to calculate let's say for example 10 Fibonacci numbers and then calculate their variance and standard deviation. At the moment I managed to get program to calculate Fibonacci numbers, but I don't know how I can load these numbers straight away to calculate variance and standard deviation. So I'm asking to input them and save them in array x, and then I calculate variance and standard deviation.
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <math.h>
using namespace std;
int n,i,one=0,two=1,ne;
//one -> first number
//two -> second number
//ne -> next number
float x[100], sum=0, avg, vari=0, vari2, sd;
int main()
{
cout << "Enter how many Fibonacci numbers you want\n" << endl;
cin >> n;
cout << "\nFirst " << n << " Fibonacci numbers are : " << endl;
for ( i=0 ; i<n ; i++ )
{
if ( i<=1 )
ne=i;
else
{
ne=one+two;
one=two;
two=ne;
}
cout << ne << endl;
}
for (i=0; i<n; i++)
{
cout << "Input your Fibonacci numbers ";
cin >> x[i];
sum = sum + x[i];
}
avg = sum/n;
for (i=0; i<n; i++)
{
vari = vari + pow((x[i] - avg),2);
}
vari2 = vari/n;
sd = sqrt(vari2);
cout << "The sum of the numbers: " << sum << endl;
cout << "The average of the numbers: " << avg << endl;
cout << "The variance of the numbers: " << vari2 << endl;
cout << "The standard deviation of the numbers: " << sd << endl;
_getch();
}
This is new code:
Everything works well, apart from variance.
I don't know why variance is calculated incorrectly.
#include <conio.h> #include <iostream>
#include <windows.h>
#include <math.h>
using namespace std;
int n,i,one=0,two=1,ne;
//one -> first number
//two -> second number
//ne -> next number
float x[10000], sum=0, avg, vari=0, vari2, sd;
int main()
{
cout << "Enter how many Fibonacci numbers you want\n" << endl;
cin >> n;
cout << "\nFirst " << n << " Fibonacci numbers are : " << endl;
for ( i=0 ; i<n ; i++ )
{
if ( i<=1 )
ne=i;
else
{
ne=one+two;
one=two;
two=ne;
}
cout << ne << endl;
sum = sum + ne;
avg = sum/n;
}
for (i=0; i<n; i++)
{
vari = vari + pow((x[i] - avg),2);
}
vari2 = vari/n;
sd = sqrt(vari2);
cout << "The sum of the numbers: " << sum << endl;
cout << "The average of the numbers: " << avg << endl;
cout << "The variance of the numbers: " << vari2 << endl;
cout << "The standard deviation of the numbers: " << sd << endl;
_getch();
}
No need to create a for loop for sum and average, their code can be implemented inside the first loop itself:
for ( i=0 ; i<n ; i++ )
{
if ( i<=1 )
ne=i;
else
{
ne=one+two;
one=two;
two=ne;
}
cout << ne << endl;
sum = sum + ne;
avg = sum/n;
Now for variance and sd, you can create a function for each to calculate their values:
double funcvari(double d){
vari = vari + pow((ne - d),2);
vari2 = vari/n;
return vari2;
}
double funcsd(double fd){
sd = sqrt(fd);
return sd;
}
Live Demo
Related
how to show output ( Number of grades above the Average + Grades above or equal the Average ) in same line i don't wont new line for each number + how to print these one time not many times , i mean because i use for loop it print Number of grades above the Average every number in one line i want just the count in one line how to do this
#include <iostream>
using namespace std;
int main()
{
int G,N;
float num[100], sum=0.0, average;
cout << "Enter the numbers of Student : ";
cin >> N;
while (N > 500 || N <= 1)
{
cout << "Error ! number of student should in range of (1 to 500)." << endl;
cout << "Enter the number again: ";
cin >> N;
}
for(G = 0; G < N; ++G)
{
cout << G + 1 << ". Enter " << G + 1 << " Mark : ";
cin >> num[G];
sum += num[G];
}
// find average
average = sum / N;
cout <<endl<< "Grades Average = " << average <<endl<<endl;
// find Grades above or equal the Average
cout<<"Grades above or equal the Average : "<< endl;
for (G = 0; G < N; ++G){
if (num[G] >= average){
cout << num[G] << endl;
}
}
// find Number of grades above the Average
cout<<endl;
for (G = 0; G < N; ++G){
if (num[G] >= average){
cout << "Number of grades above the Average : " << G + 1 << endl;
}
}
return 0;
}
There are a number of issues with your code:
num[] can hold 100 values max, but you are allowing the user to enter up to 500 values into it, thus you have the potential for a buffer overflow.
you say the valid number of students is 1 to 500, but you are preventing the user from entering 1.
lack of adequate error handling when reading the user's input.
you are outputting "Number of grades above the Average : " inside the loop for every matching grade, which you say you don't want. You should instead output it once before entering the loop, then use the loop to count the matching grades without outputting each one, then output the final count after the loop is finished.
Try this:
#include <iostream>
#include <limits>
using namespace std;
const int maxStudents = 500;
int main()
{
int G, N, nAverages = 0;
float num[maxStudents], sum = 0.0, average;
cout << "Enter the number of Students : ";
do
{
if (cin >> N)
{
if (N >= 1 && N <= maxStudents)
break;
cout << "Error ! Number of students should be in range of (1 to " << maxStudents << ")." << endl;
}
else
{
cout << "Error ! Invalid input." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter the number again: ";
}
while (true);
for(G = 0; G < N; ++G)
{
cout << G + 1 << ". Enter Mark : ";
while (!(cin >> num[G]))
{
cout << "Error ! Invalid input." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter the mark again: ";
}
sum += num[G];
}
// find average
average = sum / N;
cout << endl << "Grades Average = " << average << endl << endl;
// find Grades above or equal the Average
cout << "Grades above or equal the Average : " << endl;
for (G = 0; G < N; ++G){
if (num[G] >= average){
cout << G + 1 << ": " << num[G] << endl;
++nAverages;
}
}
// Number of grades above the Average
cout << endl << "Number of grades above the Average : " << nAverages << endl;
return 0;
}
So I'm trying to write a program that will read in ten whole numbers and outputs the sum of all numbers greater than zero, the sum of all numbers less than zero,
and the sum of all numbers, whether positive, negative, or zero.
Currently only the total sum and negative sum is adding correctly. My positive sum is always 1 or 0. Any tips to get me in the right direction would help.
Current Code:
#include <iostream>
using namespace std;
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
cout << "Number 1" << endl;
cin >> N;
cout << "Number 2" << endl;
cin >> N;
cout << "Number 3" << endl;
cin >> N;
cout << "Number 4" << endl;
cin >> N;
cout << "Number 5" << endl;
cin >> N;
cout << "Number 6" << endl;
cin >> N;
cout << "Number 7" << endl;
cin >> N;
cout << "Number 8" << endl;
cin >> N;
cout << "Number 9" << endl;
cin >> N;
cout << "Number 10" << endl;
cin >> N;
for(int i=0;i<10;i++)
{
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
cout << "The positive sum is= " << positiveSum << endl;
cout << "the negative sum is= " << negativeSum << endl;
cout << "The total sum is= " << sum << endl;
return 0;
}
After entering all Ns and when entring to the loop, you have only the last N. because after assigning the first number to N you don't process the value but you assigning the next value to N again after asking the user to enter it and so on. Use something like the following
#include <iostream>
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
for(int i=0;i<10;i++)
{
std::cout << "Number "<<i + 1<< std::endl;
std::cin >> N;
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
std::cout << "The positive sum is= " << positiveSum << std::endl;
std::cout << "the negative sum is= " << negativeSum << std::endl;
std::cout << "The total sum is= " << sum << std::endl;
return 0;
}
And see Why is "using namespace std;" considered bad practice?
I'm new to coding. I wrote the below code in C++ and I am not allow to use array.
You will create a console C++ program that uses a nested loop to enter each archer's individual end scores and then displays the total score for each archer.
I am stuck at how to calculate the total end score:
#include <iomanip>
using namespace std;
int main()
{
int Rounds = 4;
int Archers = 3;
int endScore ;
int average;
for (int a = 1; a <= Archers ; a++)
{
cout << endl << "Number " << a << " score" << endl;
int tEndScore = 0 ;
for(int i=1; i <=Rounds ; i++)
{
cout << "Round " << i << " : " ;
cin >> endScore;
while(cin.fail())
{
cout << endl << "not enter an integer " << endl ;
cout << "Please enter an integer ";
cin >> endScore;
}
tEndScore += endScore;
}
cout << endl << "The total score for 4 ends of Archer Number " << a << " is " << tEndScore << endl;
average =(double) tEndScore/Rounds;
cout << setiosflags(ios::fixed) << setprecision(2) << endl << "The average score of 4 ends of Archer Number " << a << " is " << average << endl;
}
}
This is the result after running. It will only use the last value I entered as tEndScore:
You need to shift tEndScore =+ endScore; this line inside the second for loop as
for(int i=1; i <=Rounds ; i++)
{
...
...
tEndScore += endScore;
}
And it will be a good practice (And mandatory for your code...) to initialize the tEndScore for each player as
for (int a = 1; a <= Archers ; a++)
{
tEndScore = 0;
endScore = 0;
average = 0;
...
...
}
You need to replace totalEndScore to tEndScore and totalRounds to Rounds.
Good afternoon,I was asked to make a function called void printStatistics(int n) which should print the average, sum, min and max number of n numbers passed in to it. for example if I did printStatistics(5) then I should ask the user to enter a number 5 times then print the stats.
All I managed to do was create a while loop that asked the user for a number until a negative number was entered, but I seem to have some trouble turning it into a function that asks the user to enter a number n times and then printing out the sum, average, min and max.This is what I have.
int main()
{
int value, sum;
int average, min, max;
int count;
sum = 0;
count = 0;
cout << "Enter a number: " << endl;
cin >> value;
min = value;
max = value;
while(value >= 0)
{
sum += value;
count++;
if(value > max)
max = value;
else if(value < min)
min = value;
cout << "Enter a numnber: " << endl;
cin >> value;
}
if(count == 0)
cout << "Nothing entered." << endl;
else
{
average = sum / count;
cout << "Average is " << average << endl;
cout << "Minimum is " << min << endl;
cout << "Maximum is " << max << endl;
cout << "Sum is " << sum << endl;
}
}
From what I understood from your question, you wish to have a function (printStatistics) that takes the parameter of 'int n' with 'n' corresponding to the amount of numbers the user wishes to enter. Then you ask for those specific numbers, then find the MIN, MAX, SUM, and AVERAGE of that set. If I understood this correctly, then this should do the trick:
#include <iostream>
void printStatistics(int n);
void printStatistics(int n)
{
int response;
int sum = 0;
int max;
int min;
for (int i = 1; i <= n; ++i)
{
std::cout << "Please enter the number in the " << i << " position: ";
std::cin >> response;
if (i == 1)
{
min = response;
max = response;
}
(min > response) ? min = response : min = min;
(max < response) ? max = response : max = max;
sum += response;
}
std::cout << "Sum: " << sum << std::endl;
std::cout << "Average: " << (float)sum / n << std::endl;
std::cout << "Min: " << min << std::endl;
std::cout << "Max: " << max << std::endl;
}
int main()
{
int numberCount;
do
{
std::cout << "How many numbers would you like to print? ";
std::cin >> numberCount;
}while(numberCount <= 0);
printStatistics(numberCount);
}
My problem is that I'm having to take a 5-digit integer input given by the user, and separate the integer into its individual digits. I then need to print those digits in reverse order and also print out the sum of those digits. Here is what I have coded so far to separate the 5-digit integer into its individual digits. Please note that I am limited to using integer division and modulo operators for separating the integer into its digits.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a five-digit number: ";
cin >> number;
cout << number / 10000 << " ";
number = number % 10000;
cout << number / 1000 << " ";
number = number % 1000;
cout << number / 100 << " ";
number = number % 100;
cout << number / 10 << " ";
number = number % 10;
cout << number << endl;
return 0;
}
For example, when the user inputs a 5-digit number like 77602, the program should output it as
7 7 6 0 2 and the sum of the digits is 22.
How do I go about printing this in reverse order as well as the sum of the individual digits?
Edit: Few spelling and grammatical errors.
It's a lot easier to reverse a string than an integer. It's also a lot easier to accumulate individual digits of a string containing a number than an integer. Try this:
#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char *argv[])
{
std::string number;
int accum = 0;
std::cout << "Enter a five digit number: ";
std::cin >> number;
if (number.length() != 5)
{
std::cout << std::endl << "I asked for five digits!" << std::endl;
return 0;
}
for (int i = 0; i < 5; i++)
{
if (number.at(i) < '0' || number.at(i) > '9')
{
std::cout << std::endl << "Non-integer string entered" << std::endl;
return 0;
}
accum += (number.at(i) - '0');
}
std::reverse(number.begin(), number.end());
std::cout << "Reversed: " << number << std::endl << "Sum of digits: " << accum << std::endl;
return 0;
}
This is probably easiest solution for you to understand:
#include <iostream>
using namespace std;
int main()
{
int number;
int number2;
int numberReverse;
int sum = 0;
cout << "Enter a five-digit number: ";
cin >> number;
numberReverse = number;
cout << number / 10000 << " ";
sum = sum + number/10000;
number = number % 10000;
cout << number / 1000 << " ";
sum = sum + number/1000;
number = number % 1000;
cout << number / 100 << " ";
sum = sum + number/100;
number = number % 100;
cout << number / 10 << " ";
sum = sum+number/10;
number = number % 10;
cout << number << endl;
sum = sum+number;
cout << "Reverse: " << endl;
number2 = numberReverse%10;
cout << number2 << " ";
number2 = (numberReverse/10)%10;
cout << number2 << " ";
number2 = (numberReverse/100)%10;
cout << number2 << " ";
number2 = (numberReverse/1000)%10;
cout << number2 << " ";
number2 = (numberReverse/10000)%10;
cout << number2 << endl;
cout << "Sum is " << sum;
return 0;
}
A simple solution using the same logic and tools of your code..
int sum = 0;
for(int i = 0; i < 5; ++i)
{
int digit = number % 10;
sum += digit;
cout << digit << " ";
number /= 10;
}
cout << "\nSum of digits: " << sum << "\n";
I think, you need to do something like this:
int number;
... some IO operations ...
std::vector<int> digits;
do {
digits.push_back(number % 10);
number /= 10;
} while (number);
After this all you need to do is 2-3 loops through vector digits
for (std::size_t k = digits.size() - 1; k >= 0; --k)
std::cout << digits[k] << " ";
std::cout << std::endl;
for (std::size_t k = 0; k < digits.size(); ++k)
std::cout << digits[k] << " ";
std::cout << std::endl;
int sum = 0;
for (std::size_t k = 0; k < digits.size(); ++k)
sum += digits[k];
std::cout << sum;