12 Replies - 1067 Views - Last Post: 06 June 2011 - 11:26 AM Rate Topic: -----

#1 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

C++ File read into array help

Posted 06 June 2011 - 06:33 AM

I am getting ind errors on this program. The homework problem is to take a text file and go through that text file count the numbers in the file, find the average, find the squared average, and find the variance. Then it needs to go back and do it again for only positive values.

I am getting exponential numbers for the first squared average and variance, while the second part is not even counting the positive numbers. I am looking for help, here's a example of the file to read.

-6
1
-5
-7
-7
6
3
-4
-1
-7
-7
1
5
-2
-4
-3
8
1
-4
9

My current code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main() {
    cout << "Part One" << endl;
    double arr[100], avg, x;  //var's are: arr[0], arr[1] ... arr[19]
    int count, i;
    ifstream inf;
    inf.open("c:\\temp\\program4_data.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\program4_data.txt";
       system("pause");
       exit(0);         
    }  
    count=0;
    while (inf >>arr[count]) count++;     
    double k=0.00;
    for(int i=0; i<count;i++)
    k+=arr[i];
    avg=k/count;
    double sqravg1;
    double sqravg;
    double var1;
    x=20;
    sqravg1=0;
    for(int i=0; i<count; i=i+1)
        sqravg1+=((arr[i+1]*arr[i+1]));
    sqravg=sqravg1/count;
    cout << "Count" << setw(23) << "=" << count << endl;
    cout << "Average" << setw(21) << "=" << avg <<endl;
    cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
    var1=sqravg-avg;
    cout << "Variance" << setw(20) << "="  << var1 << endl;
    cout << endl;
    cout << "Part 2" << endl;
    cout << endl;
    count=0;
    inf.close();
    inf.open("c:\\temp\\program4_data.txt");
    while (inf >>arr[count])
        if (arr[count]>0)count++;     
    inf.close();
    double avg1;
    avg1=0;
    for(int i=0; i<count; i=i+1)
        avg1+=((arr[i+1])) ;
    avg = avg1/count;
    sqravg1=0;
    for(int i=0; i<count; i=i+1)
        sqravg1+=((arr[i+1]*arr[i+1]));
    sqravg=sqravg1/count;
    cout << "Count" << setw(23) << "=" << count << endl;
    cout << "Average" << setw(21) << "=" << avg <<endl;
    cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
    var1=sqravg-avg;
    cout << "Variance" << setw(20) << "="  << var1 << endl;
    cout << endl;
    system("pause");
}


Is This A Good Question/Topic? 0
  • +

Replies To: C++ File read into array help

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,300
  • Joined: 25-December 09

Re: C++ File read into array help

Posted 06 June 2011 - 06:56 AM

Look at the following lines:

    for(int i=0; i<count; i=i+1)
       sqravg1+=((arr[i+1]*arr[i+1]));


When i is == count - 1, what is the value of i + 1?

You are accessing your array out of bounds.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ File read into array help

Posted 06 June 2011 - 06:56 AM

Three problems unrelated to your main concern, that compilation using useful flags will show you:
- you never use the i variable you declared at the top of your main(),
- you assigned value to, but never used variable x, declared at the top of your main(),
- you never included cstdlib, and hence, there are no system() or exit() functions declared.

Also, when you exit the program showing an error message that you couldn't open your file - shouldn't you return something else than 0 to the OS?

Did you cover functions yet? Because you should have placed most of that code into separate functions that would calculate the averages, and then just use them in main().

You wrote:
for (int i = 0; i < count; i = i + 1)
    sqravg1 += ((arr[i + 1] * arr[i + 1])); // [i + 1]? why?

Why exactly do you add 1 to i there?

You also do it later on:
    for (int i = 0; i < count; i = i + 1)
        avg1 += ((arr[i + 1]));
//...
    for (int i = 0; i < count; i = i + 1)
        sqravg1 += ((arr[i + 1] * arr[i + 1]));
Note, that you pretty much coded the same thing twice - and you calculate average different the first, and the second time. ;) This is alleviated by using functions.

http://www.cplusplus...rial/functions/
http://www.cplusplus...ial/functions2/
Was This Post Helpful? 1
  • +
  • -

#4 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: C++ File read into array help

Posted 06 June 2011 - 07:33 AM

I fixed the unused functions and the unneeded i+1. The first part is now working correctly, the second still isn't. We have not learned functions yet. In regards to the second half it looks like it having problems reading the file any idea why?

This post has been edited by defcon18: 06 June 2011 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ File read into array help

Posted 06 June 2011 - 08:39 AM

Did you also fix the + 1 bug in part 2 loops? Why do you think it isn't working correctly? For me, if I have all positive numbers in the file, then there are identical results. For file "-5 -5 5 5" it calculates everything correctly.
Was This Post Helpful? 0
  • +
  • -

#6 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: C++ File read into array help

Posted 06 June 2011 - 08:48 AM

I did change the part 2 loop, when I try and run the program it does the first run through, then it errors out on the second part to count=0, average, sqraverage, variance all say -1.f.

Updated code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
    cout << "Part One" << endl;
    double arr[100], avg;  //var's are: arr[0], arr[1] ... arr[19]
    int count;
    ifstream inf;
    inf.open("c:\\temp\\program4_data.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\program4_data.txt";
       system("pause");
       exit(0);         
    }  
    count=0;
    while (inf >>arr[count]) count++;     
    double k=0.00;
    for(int i=0; i<count;i++)
    k+=arr[i];
    avg=k/count;
    double sqravg1;
    double sqravg;
    double var1;
    sqravg1=0;
    for(int i=0; i<count; i=i+1)
        sqravg1+=((arr[i]*arr[i]));
    sqravg=sqravg1/(double)count;
    cout << "Count" << setw(23) << "=" << count << endl;
    cout << "Average" << setw(21) << "=" << avg <<endl;
    cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
    var1=sqravg-avg;
    cout << "Variance" << setw(20) << "="  << setprecision(2) << var1 << endl;
    cout << endl;
    cout << "Part 2" << endl;
    cout << endl;
    count=0;
   	while (inf >>arr[count])
    if (arr[count]>0)count++;     
    inf.close();
    double avg1;
    avg1=0;
    for(int i=0; i<count; i=i+1)
        avg1+=((arr[i])) ;
    avg = avg1/count;
    sqravg1=0;
    for(int i=0; i<count; i=i+1)
        sqravg1+=((arr[i]*arr[i]));
    sqravg=sqravg1/count;
    cout << "Count" << setw(23) << "=" << count << endl;
    cout << "Average" << setw(21) << "=" << avg <<endl;
    cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
    var1=sqravg-avg;
    cout << "Variance" << setw(20) << "="  << var1 << endl;
    cout << endl;
    system("pause");
}

This post has been edited by defcon18: 06 June 2011 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ File read into array help

Posted 06 June 2011 - 08:55 AM

You deleted inf.close() call. You should close and reopen the file (or move the g-pointer to the beginning of the file) after you looped through it in part 1.

This post has been edited by Xupicor: 06 June 2011 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

#8 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: C++ File read into array help

Posted 06 June 2011 - 09:02 AM

Still doing it:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
    cout << "Part One" << endl;
    double arr[100], avg;  //var's are: arr[0], arr[1] ... arr[19]
    int count;
    ifstream inf;
    inf.open("c:\\temp\\program4_data.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\program4_data.txt";
       system("pause");
       exit(0);         
    }  
    count=0;
    while (inf >>arr[count]) count++;     
    double k=0.00;
    for(int i=0; i<count;i++)
	k+=arr[i];
    avg=k/count;
    double sqravg1;
    double sqravg;
    double var1;
    sqravg1=0;
    for(int i=0; i<count; i=i+1)
        sqravg1+=((arr[i]*arr[i]));
    sqravg=sqravg1/(double)count;
    cout << "Count" << setw(23) << "=" << count << endl;
    cout << "Average" << setw(21) << "=" << avg <<endl;
    cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
    var1=sqravg-avg;
    cout << "Variance" << setw(20) << "="  << setprecision(2) << var1 << endl;
    cout << endl;
    cout << "Part 2" << endl;
    cout << endl;
	inf.close();
    count=0;  
    inf.open("c:\\temp\\program4_data.txt");
    while (inf >>arr[count])
    if (arr[count]>0)count++;    
	inf.close();
    double avg1;
    avg1=0;
    for(int i=0; i<count; i=i+1)
        avg1+=((arr[i])) ;
    avg = avg1/count;
    sqravg1=0;
    for(int i=0; i<count; i=i+1)
        sqravg1+=((arr[i]*arr[i]));
    sqravg=sqravg1/count;
    cout << "Count" << setw(23) << "=" << count << endl;
    cout << "Average" << setw(21) << "=" << avg <<endl;
    cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
    var1=sqravg-avg;
    cout << "Variance" << setw(20) << "="  << var1 << endl;
    cout << endl;
system("pause");
}

This post has been edited by defcon18: 06 June 2011 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5770
  • View blog
  • Posts: 12,582
  • Joined: 16-October 07

Re: C++ File read into array help

Posted 06 June 2011 - 09:07 AM

There's really no reason to load the data twice. Or to loop through it so many times. Use some functions.

I'd have a couple that looked like this:
int loadData(double arr[], const char *filename) {
	ifstream inf;
	inf.open(filename);
	if (!inf) {
		cout << "problem with input file: " << filename;
		return -1;
	}  
	int count=0;
	while (inf >>arr[count]) { count++; }
	inf.close();
	return count;
}

void showData(int count, int total, int sqTotal) {
	double avg = total/(double)count;
	double sqravg = sqTotal/(double)count;
	double var = sqravg-avg;
	cout << "Count" << setw(23) << "=" << count << endl;
	cout << "Average" << setw(21) << "=" << avg <<endl;
	cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
	cout << "Variance" << setw(20) << "="  << var << endl;
	cout << endl;
}



Hope this helps.

This post has been edited by baavgai: 06 June 2011 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#10 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: C++ File read into array help

Posted 06 June 2011 - 09:12 AM

The main problem is we haven't learned functions.
Was This Post Helpful? 0
  • +
  • -

#11 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ File read into array help

Posted 06 June 2011 - 09:28 AM

It works for me, so... Are you sure you recompiled the code?
Was This Post Helpful? 0
  • +
  • -

#12 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: C++ File read into array help

Posted 06 June 2011 - 10:30 AM

So it compiles and runs correctly in visual studio, but when I try and compile and run in dev c++ which my instructor uses it fails on the second part with -1.$ as the answer. I know Dev C++ is highly outdated but I don't have the ability to make my instructor use a newer compiler.
Was This Post Helpful? 0
  • +
  • -

#13 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: C++ File read into array help

Posted 06 June 2011 - 11:26 AM

Dev-C++ uses GCC 3.4.2 by default, it was all trendy in September of 2004, it's not that hot anymore, since we have GCC 4.6.0 ;) I can't really see why it wouldn't work even on 3.4.1...

Maybe instead of reopening the file you could try to set the g-pointer to the beginning of the file, like I said earlier:
inf.seekg(0, ios::beg);

http://www.cplusplus.../istream/seekg/

You should also clear error state by using, well, .clear()
http://www.cplusplus...ream/ios/clear/

This post has been edited by Xupicor: 06 June 2011 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1