help with using datafiles in arrays

Pages: 12
You need #include <numeric> for std::accumulate.
Good! But can you explain every statement that is in your code?
These are the errors Im getting

In function 'int main()':
warning: 'auto' will change meaning in C++0x; please remove it
error: ISO C++ forbids declaration of 'minmax' with no type
error: 'minmax_element' is not a member of 'std'
error: 'begin' is not a member of 'std'
error: 'end' is not a member of 'std'
error: 'begin' is not a member of 'std'
error: 'end' is not a member of 'std'
error: expected primary-expression before '[' token
error: expected primary-expression before 'int'
expected unqualified-id before 'int'
error: request for member 'second' in 'minmax', which is of non-class type 'int'


I am not used to using std:: and my teacher never really used it in class either. So when I submit it all these errors showed up.
Last edited on
std is the standard namespace. Everything standard is in it: std::cout, std::cin, std::rand(), std::string, std::vector...
This errors shows that compiler is outdated and should be updated.
I have the 2010 compiler. When I submit it, it says syntax errors are found and those are it. I am up to date so that's not the problem.
Wait. Do you send code to somewhere else? If so that means that they are using outdated compiler. Does this code works for you?
"submit" sounds like he means "when I try to compile"

VS2010 is not "up to date"

warning about auto changing in C++0x means that the compiler is not in C++11 mode.
Yes it works for me. But I got the hard copy of Microsoft Visual C++ 2010 from my professor and others have already finished it so trust me its in the code, and has nothing to do with the compiler. I am not used to this std::vector and std::rand() my professor shows examples and has Ive never seen these on any of them.
Last edited on
cluterbug are you in sims class at UC?
Last edited on
At where? I go to a community college in florida.
im in kentucky I have the same exact program assigment word for word lol
so trust me its in the code, and has nothing to do with the compiler
http://ideone.com/3Gp9gv
Code is completely working.
(I used std::cin instead of file because ideone won't lt you read from files)

Your compiler is outdated: http://cplusplus.com/articles/EzywvCM9/ try to compile some of examples and you cannot. Because there were changes to C++ standard in 2011. Which VC2010 does not know about.
Here I figured it out without using the whole std:: thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
 
int sum(int arr[],int size);
double average(int arr[],int size); 
int smallestAt(int arr[],int size); 
int largestAt(int arr[],int size); 
void getCounts(int arr[],int size,int count[]);
 

int main()
{
    const int STUDENTS=1000;
    int scores[STUDENTS];
    ifstream infile("scores.txt");
    double avg;
    int minScore,maxScore;
    int counts[4]={0};
    
    if(!infile)
	{
        cout<<"File failed to open.";
        exit(1);
    }
    
    for(int i=0;i<STUDENTS;i++)
	{
        infile>>scores[i];
    }

    avg=average(scores,STUDENTS);
    minScore=scores[smallestAt(scores,STUDENTS)];
    maxScore=scores[largestAt(scores,STUDENTS)];
    getCounts(scores,STUDENTS,counts);
    
    cout<<"Average:\t"<<avg<<endl
        <<"Min. Score:\t"<<minScore<<endl
        <<"Max. Score:\t"<<maxScore<<endl
        <<"Counts:"<<endl
        <<"Excellent(95-100): "<<counts[0] <<endl
        <<"Satisfactory(80-94): "<<counts[1]<<endl
        <<"Normal (50-79): "<<counts[2]<<endl
        <<"Needs Improvement (0-49): "<<counts[3]<<endl;
 
    cin.get();
        return 0;
}
 
int sum(int arr[],int size)
{
        int total=0;
        for(int i=0;i<size;i++)
		{
                total+=arr[i];
        }
        return total;
}
 
double average(int arr[],int size)
{
        int total=sum(arr,size);
        return (double)total/(double)size;
}
 
int smallestAt(int arr[],int size)
{
        int small=0;
        for(int i=1;i<size;i++)
		{
                if(arr[small]>arr[i])
                        small=i;
        }
        return small;
}
 
int largestAt(int arr[],int size)
{
        int large=0;
        for(int i=1;i<size;i++)
		{
                if(arr[large]<arr[i])
                        large=i;
        }
        return large;
}
 
void getCounts(int arr[],int size,int count[])
{
    int x;
    for(int i=0;i<size;i++)
	{
        x=arr[i];
        
        if(x<=100 && x>=95)       
            count[0]++;
        else if(x<=94 && x>=80)       
            count[1]++;
        else if(x<=79 && x>=50)       
            count[2]++;
        else
            count[3]++;
        
    }
    return;
}
Congratulation. You did what everyone expect of you: stop asking to do everything for you and do it yourself. Isee that you had used functions you have written for your other assigment. That is good. This is the meaning of functions: compact reusable pieces of code. You saved your time there.
Some notes:
1) I told you about using namespace std; and c-style casts already
2) In get coun function you don't need to check if x<=94 or x<=79 in your else statements: It will always be true — if it isn't then previous if statements would already process it. Also you on't really need x<=100 in your first if: number is guaranteed to be in range [0;100] by assigment conditions.
Topic archived. No new replies allowed.
Pages: 12