Utilizing Different Areas of an Array

in this program you can use an input file that has a record of a bird watching event to do a few calculations with the program. its a project for school, and i have the program inputting the data and displaying the name of the bird, and the amount of times its seen, but it wont utilize the data from the input file in any other part of the program. can anyone shed some light onto what i need to change?

the program is a modification of a program that was prewritten to do a similar task
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Project 4 
// Program to manage a list of bird species and counts

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int ARRAY_SIZE = 20;   //  sales for person i go into pos i - 1 


void printResults(const double x[],int sizeX);
double  sumArray(const double x[],int sizeX);
int indexLargestElement(const double list[], int listSize);


int main()
{
    double birds[ARRAY_SIZE] ; 
    string location;         // Location of the watch        
    int sub;                 // loop control variable 
    string species;          // Species of bird
    double times;            // number of times species was seen
    int winner ;             // subscript of largest sale 
    
    
    string filename;             // name of the input file                     
    ifstream inFile;             //input file stream variable
    
    cout << "Enter the name of the input file:  " ;
    cin >> filename;
    
    inFile.open(filename.c_str ()  ); 
  //   inFile.open(filename ); 
 
    if (!inFile) 
    {    
         cout << "bummer file name \n\n";
         system ("pause");
         return 1;
    }
        
    cout << filename << " was opened" << endl << endl; 
                    
    sub = 0;
    while ( sub < ARRAY_SIZE ) 
    {
          birds [sub] = 0;
          sub++;
    }      
        
    
    
    getline (inFile, location);
    inFile >> species >> times;
    cout << "The watch was held at: " << location << "." << endl << endl;   
    while (inFile ) 
    {
         cout << species << " was seen " << times << " times" << endl;
         inFile >> species >> times; 
    }    
     
   
   
    cout << "\n\n";
    printResults (birds, ARRAY_SIZE);               
    cout << endl;  
    

    cout << "\n\nTotal sales for all employees $" 
         << sumArray ( birds,  ARRAY_SIZE)
         << endl << endl ;
   
    winner = indexLargestElement ( birds ,ARRAY_SIZE)  + 1;
     cout << "High seller is salesperson " << winner << endl;
    
         
    system ("pause");
    return 0;
}

    
    //Function to print the elements of an int array.
    //The array to be printed and the number of elements 
    //are passed as parameters. The parameter listSize
    //specifies the number of elements to be printed.
    // Info for salesperson # n is listed in sales[n - 1]
    // There is no salesperson 0. 
void printResults(const double list[], int listSize)
{
    int index;
    
    cout << "Species\t\tTimes Seen" << endl << endl;

    for (index = 0; index < listSize; index++)
        cout << index + 1  << "\t\t" << list[index] << endl;
}

    //Function to find and return the sum of the
    //elements of an int array. The parameter listSize
    //specifies the number of elements to be added.
double  sumArray(const double list [], int listSize)
{
    int index;
    double  sum = 0;

    for (index = 0; index < listSize; index++)
        sum = sum + list[index];

    return sum;
}

    //Function to find and return the index of the first
    //largest element in an int array. The parameter listSize
    //specifies the number of elements in the array.
int indexLargestElement(const double list[], int listSize)
{
    int index;
    int maxIndex = 0; //Assume the first element is the largest

    for (index = 1; index < listSize; index++)
        if (list[maxIndex] < list[index])
            maxIndex = index;

    return maxIndex;
}



the input file that i am using for the program is here:

http://cs.odu.edu/~cs150/projects/project%204/data%20files/one.txt
OP wrote:
but it wont utilize the data from the input file

you did not put the input data into your list, so the functions that take the list as parameter will likely print some garbage/datas that you didn't expect
where do i add the input data into my list though?
1. create an structure/class that will hold name of bird and frequency it was seen, create an array of objects of the structure, sort the array by frequency
- but probably not what you need here :\
2. pseudo code
get name of bird and frequency from input list
->assign them to name and max_frequency
get another name of bird and freq data
->compare the frequencies, save the bigger one and the name it was associated with
do this until end of input


imo you don't even need the functions (?)
Last edited on
i removed one of the functions, but because of the fact that this is for a class, it is required that we dont perform any of the calculations within the main function, so we have to have some other functions. and i think i understand what you mean, but could you elaborate a little? sorry, im not new to programming, but i dont really understand the whole array and struct concept, it confuses me a bit.
but could you elaborate a little? ... i dont really understand the whole array and struct concept, it confuses me a bit.

well structs are user-defined data types (like int, float etc.), it's customised to a user's needs -and you can think of structs as sets i.e {name, DOB} that groups different data together.
the great things about C++ (and C) user-defined types is that you can use/manipulate them like normal types:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int intObject; //declaring an integer object

//I trust you are at least familiar with how to declare and define structs,
//if not, find the tutorials and read the books and lecture notes
StructName  structObject;

//and you can declare an array of user defined data
StructName structObject[10]; //structObject[0]...structObject[9]

//you can pass them into functions...
void FnDisplayStruct(StructName sn);

//or create pointers to them
StructName *pStructObject;


okay, so my code has been changed a bit, and im starting to understand how to work with structs right now im trying to make a separate function to add up all the times the birds were seen. i have it being done in the main function here, but i need to put it in a separate function, how do i predefine the parameters of a function using a struct? what parts of the struct need to go into the parameters at the top and the bottom?

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
// Project 4 
// Program to manage a list of bird species and counts


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

const int ARRAY_SIZE = 20;


int birdCalculations(string );


int main()
{
    struct birdType         // struct to input all the different bird data
    {
           string birdName;
           double timesSeen;
    };
    birdType Bird, newBird;    
    double birds[ARRAY_SIZE] ; 
    string location;         // Location of the watch        
    int sub;                 // loop control variable 
    string species;          // Species of bird
    double times;            // number of times species was seen
    int winner ;             // species most seen
   
    
    
    string filename;             // name of the input file                     
    ifstream inFile;             //input file stream variable
    
    cout << "Enter the name of the input file:  " ;
    cin >> filename;
    
    inFile.open(filename.c_str ()  ); 
  //   inFile.open(filename ); 
 
    if (!inFile) 
    {    
         cout << "bummer file name \n\n";
         system ("pause");
         return 1;
    }
        
    cout << filename << " was opened" << endl << endl; 
                    
    
    Bird.timesSeen = 0;
    getline (inFile, location);
    inFile >> newBird.birdName >> newBird.timesSeen;
    cout << "The watch was held at: " << location << "." << endl << endl;   
    while (inFile ) 
    {
         cout << newBird.birdName << " was seen " << newBird.timesSeen << " times" << endl;
         Bird.timesSeen = newBird.timesSeen + Bird.timesSeen;
         inFile >> newBird.birdName >> newBird.timesSeen; 
    }    
    
    cout << Bird.timesSeen;
   
   
    cout << "\n\n";
         
    system ("pause");
    return 0;
}


see im trying to make the separate function called birdCalculations, preferably a void function that simply adds up the counts like it does in the main part. but i dont know what to put in the top where i declare the function and the bottom where i write the function
and by the way thanks so much for helping me to understand this!

how do i predefine the parameters of a function using a struct?
let's say we want to display a struct:
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
//top???
//declaration, void because we're not expecting to return anything
//StructName st, passing the struct by value i.e make a copy of the original struct
void Display(StructName st); 

//calling Display(StructName) in main for e.g
{
    //...other codes

    StructName stObject;
    stObject.first_member = first_member_value;
    stObject.second_member = second_member_value;
    //etc.
    Display(stObject);
}

//bottom???
//definition
void Display(StructName st)
{
    //exact duplicate of the struct passed, provided we already fill it somewhere else!
    std::cout << st.first_member << std::endl; //outputs first_member_value to std::cout
    std::cout << st.second_member << std::endl; //outputs second_member_value to std::cout
    //etc.
}


Topic archived. No new replies allowed.