Standard Deviation Code Output Error - visual-c++

I can't see my output from this code I have written. I'm trying to calculate the mean and standard deviation of a set of numbers from a file. I'm lost as to what the problem is and I won't know if my code is right until I can see output. Here's what I have so far:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
// Declare Variables
int n;
int xi;
int sdv;
int sum;
int sum2;
int sum3;
int mean;
// Declare and open input files
ifstream inData;
inData.open("score.dat");
if (!inData) // Incorrect Files
{
cout << "Cannot open input file." << endl;
return 1;
}
// Initialize variables and output
inData >> xi;
n = 0;
sum = 0;
sum3 = 0;
sdv = 0;
mean = 0;
while (inData)
{
sum += xi;
sum2 = sum * sum;
sum3 += (xi * xi);
mean = sum / n;
sdv = (sum3 - sum2) / (n * (n - 1));
inData >> xi;
}
// Print commands
cout << "The Standard Deviation of the Tests is:" << sdv << endl;
cout << "The Mean of the Tests is: " << mean << endl;
inData.close();
system("pause");
return 0;
}

After running your code I found a few bugs. You probably don't get any output because the program crashes the first time through the while loop on the line:
mean = sum / n;
due to a divide by zero error.
The other bug is that you don't increase (increment) the value of n in your loop, so you always only have one number.
Adding an n++ at the beginning of your loop will fix that
while (inData)
{
n++;
sum += xi;
...
But you still get a divide by zero on the sdv when n == 1:
sdv = (sum3 - sum2) / (1 * (1 - 1));
if you add a condition before the division it will work:
if (n >= 2)
sdv = (sum3 - sum2) / (n * (n - 1));
Look into debugging tools like gdb to help catch things like this.

Related

For a given integer n, find the number following n that is a multiple of 10

For a given integer, n, find the number following n that is a multiple of 10.
I wrote code that seems to be correct, but the site reviewer writes that it is only 90% correct. What could be the problem?
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n == 0) n = n + 10;
while (n % 10 != 0) n++;
cout << n;
}
Your code doesn't work if the given n is itself a multiple of 10, in which case it returns 'itself' rather than the next multiple of 10 (i.e. 1 in 10 possible cases = 90% success rate).
To fix this, increment the n before testing for a multiple of 10:
#include <iostream>
int main()
{
int n;
std::cin >> n;
while (++n % 10 != 0)
;
std::cout << n;
return 0;
}
Alternatively, you can avoid the loop entirely. The following will work for both positive and negative numbers (it's a lot simpler if you don't cater for negative numbers):
n += ( n >= 0 ? 10 : 0 ) - (n % 10);

C++ trouble with outputing sum/avg

am taking a class of C++ and been asked to write a program that reads integers from a file.
first request is to output all of the integers in one line
and the second request is to output the average of the integers
ive tried what has been written in the book, and when I try to cout the sum or the average, it output the addition number by number not just the total
how can I fix this? i want the simplest code possible, I dont want anything that we didnt take in class yet
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
int num;
ifstream infile;
ofstream outfile;
outfile.open("Answer.txt");
infile.open("DataFile2.txt");
infile >> num;
while (infile)
{
outfile << num << " ";
infile >> num;
}
infile.close();
infile.open("DataFile2.txt");
int sum = 0;
while (infile)
{
double avg;
infile >> num;
sum = sum + num;
avg = sum / 14;
cout << endl << sum << avg;
}
}
There are a lot of mistakes in your code.You should declare the avg variable outside the while loop.Also, you should calculate the average and print it after the while loop has finished looping.Also you are dividing the sum by 14 (constant) which is not good, since you don't know how many integers are in the file.
The code should look something like this:
int sum = 0;
double avg;
while (infile) {
infile >> num;
sum = sum + num;
}
avg = sum / 14;
cout << endl << sum << avg;

Putting values from one Array into another

I'm at a loss here, so I am looking for any hints to point me in the right direction. I can't figure out how to input the Celsius values that I converted from the Fahrenheit temperatures into the centigrade array. I tried to work in another for loop for that very purpose but it only outputs the last value for C after the calculation from the first for loop. Any ideas? Thanks in advance.
// Temperature Converter
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
int main()
double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
double centigrade[] = { 0 }, C(0);
int i(0);
cout << setw(13) << "Farenheit " << setw(9) << " Centigrade";
cout << endl;
for (double t : temps)
{
C = (t - 32) * 5 / 9;
cout << setw(10) << t << setw(12) << C;
cout << endl;
}
for (i = 0; i <= 12; i++)
{
centigrade[i] = C;
cout << centigrade[i] << endl;
}
return 0;
}
Here is a full working example based on the other answer.
#include <iostream>
using std::cout;
using std::endl;
int main() {
double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
const int count = sizeof(temps) / sizeof(temps[0]);
double centigrade[count];
for (int i = 0; i < count; i++) {
centigrade[i] = (temps[i] - 32) * 5 / 9;
cout << centigrade[i] << endl;
}
return 0;
}
If you want to work without an explicit indexing loop, then replace double centigrade[count]; with std::vector<double> centigrade, and replace the loop with:
for (double t : temps)
centigrade.push_back((t - 32) * 5 / 9);
If you then wanted an array back for some reason, you could use this trick to get an array back:
double* array_version = &centigrade[0];
Store values in the array in the first loop itself..
for (i=0;i<=12;i++)
{
centigrade[i]= (temps[i] - 32) * 5 / 9;
cout << setw(10) << temps[i] << setw(12) << centigrade[i];
cout << endl;
}
U can generalize the for loop by finding the size of temps array dynamically..maybe
sizeof (temps) / sizeof (temps[0]);
Also allocate memory for centigrade array accordingly.
I am adding a new answer based on clarifications to the OP's question, rather than updating my existing answer, because I feel the context is more clear this way.
If you want to use a range-based loop, and avoid std::vector, there is a way to do it, but this solution is more in the spirit of C thanC++, because it uses pointer arithmetic.
#include <iostream>
using std::cout;
using std::endl;
int main() {
double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
const int count = sizeof(temps) / sizeof(temps[0]);
double centigrade[count];
double * walker = centigrade;
for (double t : temps)
*walker++ = (t - 32) * 5 / 9;
// verify results by printing.
for (double t: centigrade)
cout << t << endl;
return 0;
}

How to solve http://www.spoj.com/problems/MST1/ in n is 10^9

Using Bottom to up DP approach, I am able to solve the problem How to solve http://www.spoj.com/problems/MST1/ upto 10^8.
If input is very large n upto 10^9. I will not be able to create lookup table for upto 10^9. So what will be better approach to solve the problem ?
Is there any heuristic solution ?
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int main()
{
const int N_MAX = 20000001;
int *DP = new int[N_MAX];
DP[1] = 0;
for (int i = 2; i < N_MAX; i++) {
int minimum = DP[i - 1];
if (i % 3 == 0) minimum = min(minimum, DP[i/3]);
if (i % 2 == 0) minimum = min(minimum, DP[i/2]);
DP[i] = minimum + 1;
}
int T, N; cin >> T;
int c = 1;
while (T--) {
cin >> N;
cout << "Case " << c++ << ": " << DP[N] << endl;
}
delete[] DP;
}

Why am I getting an assertion error?

#include <iostream>
using namespace std;
int main ()
{
int size = 0;
int* myArray = new int [size + 1];
cout << "Enter the exponent of the first term: ";
cin >> size;
cout << endl;
for (int i = size; i >= 0; --i)
{
cout << "Enter the coefficient of the term with exponent "
<< i << ": ";
cin >> myArray[i];
}
for (int i = size; i >= 0; --i)
{
cout << i << endl;
}
return 0;
}
Why am I getting an assertion error on input greater than 2? This is the precursor to a polynomial program where the subscript of the array is the power of each term and the element at array[subscript] is the coefficient.
Your array is allocated to be an int[1]. It needs to be allocated after you read in the size value.
You are initializing your array when size = 0, giving an array size of 1
You get your assertion error when you go outside of the array bounds (1).
myArray always has size 0 + 1 = 1. i starts out at whatever the user inputted, and the first array access you make is myArray[i]. So, say the user inputs 5, your array has size 1 and you access myArray[5]. It will fail!
I would allocate the array AFTER you input size.

Resources