Array Assistance

Pages: 12
By assigning smallest/largest to be the first element of the array, if the array has at least a size of 1. (If the size of the array is 0, then that's a very edge case scenario, but I guess I would return MIN for largestNumber, and MAX for smallestNumber).

I think your smallest/largest functions are fine as-is, as long as you don't have negative numbers. (Except for what I said earlier about passing the position by reference.)
Last edited on
confused again...

1
2
3
4
5
6
7
8
9
10
11
12
13
//Function that finds the largest value and its position
double LargestNumber(int ary[], int counter, int& position)
{
    int largest=ary[0];
    for(int i=0;i<counter;i++)
    {
        if(largest<ary[i])
        largest=ary[i];
        position=i;
    }

    return largest;
}

1
2
3
4
5
6
7
8
9

    //Function call to determine min/max number of the array.
    largest=LargestNumber(MyArray,counter,pos);
    smallest=SmallestNumber(MyArray,counter,pos);

    out << "\nLargest Number of the Array is: "<<largest
        << " Position: " << pos
        << "\nSmallest Number of the Array is: "<<smallest
        << " Position: ";

unsure how to call just the position, would i need to make a separate function?
this currently sets the pos variable as junk.
Last edited on
you has it, its in position.
if you call it with
x = LargestNumber(a, size, pos);
pos is your answer.

except you have a bug. there is only one statement in that if.... position = i is not in the if...
Last edited on
Damn...... I knew it was something simple... I apologize! I should of caught that error.
Is that how functions work? I can call it within my main and I can choose which parameter within the function call I want to use?

with the prototypes is it necessary to place them in order of being called in main? Or is it ok to re-arrange?

Also, is it important at all to have main first above all other functions?
Andy mentioned before I shouldn't use improper names for functions and variables. In a function I'm aware that the variable is only good for that function. So names can be repeated. But is it wise to use the same name within various functions?
Last edited on
Code as of now
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

//Constant for program handling
const int MAX = 100;

//Prototypes
double Total(int[],int);
void ReadArray(int[],int&);
void ZeroOutEvens(int[],int);
void PrintArray(int[], int&);
double AverageArray(int[], int);
double AverageArray(double, int);
void NegativeToPositive(int[],int);
double LargestNumber(int[],int, int&);
double SmallestNumber(int[],int, int&);
int LargerThanAverage(int[],int, double);
int SmallerThanAverage(int[], int, double);


//File I/O
ifstream inf("Array.in");
ofstream out("Array.out");
ifstream in2("Parameters.in");

int main()
{
    //Variables
    int MyArray[MAX],counter,large,largest,
        smallest,pos1,pos2,small,zero;

    double average,sum;

    //Outfile Header
    out << "\tArray Work-Out Program\n"
         << "First Array: \n";

    //Function call to read in the numbers and print to file
    ReadArray(MyArray, counter);
    PrintArray(MyArray,counter);

    //Function call to average the array
    sum=Total(MyArray,counter);
    average=AverageArray(sum, counter);
    out << "Sum = " << sum << " COUNT = " << counter
        << " AVERAGE = " << average;

    //Function call for number larger/smaller than average
    large=LargerThanAverage(MyArray,counter,average);
    small=SmallerThanAverage(MyArray,counter,average);
    out << "\n\nValues LARGER than AVERAGE: " << large
        << "\nValues SMALLER than AVERAGE: " << small;

    //Function call to change negatives to positive
    NegativeToPositive(MyArray,counter);
    PrintArray(MyArray,counter);
    sum=Total(MyArray,counter);
    average=AverageArray(sum,counter);
    out << "Sum = " << sum << " COUNT = " << counter
        << " AVERAGE = " << average;

    //Function call to determine min/max number of the array.
    largest=LargestNumber(MyArray,counter,pos1);
    smallest=SmallestNumber(MyArray,counter,pos2);
    out << "\nLargest Number of the Array is: " << largest
        << " Position: " << pos1
        << "\nSmallest Number of the Array is: " << smallest
        << " Position: " << pos2;

    //Function call to zero out even
    ZeroOutEvens(MyArray,counter);
    PrintArray(MyArray,counter);
    sum=Total(MyArray,counter);
    average=AverageArray(sum,counter);
    out << "Sum = " << sum << " COUNT = " << counter
        << " AVERAGE = " << average;


    out.close();
    inf.close();
    in2.close();
    return 0;
}

//Function to read values into the array
void ReadArray(int ary[], int& counter)
{
    int num;
    counter=0;
    while(inf >> num && counter < MAX)
    {
        ary[counter] = num;
        counter++;
    }
}

//Function to print the data to the desired file
void PrintArray(int bry[],int& c)
{
    out << setprecision(2) << fixed;

    for(int i=0; i<c; i++)
    {
        out << bry[i] << ", ";
        if((i+1)% 10 == 0)
        {
            out << endl;
        }
    }
}

//Function that totals up the numbers of the infile
double Total(int ory[], int z)
{
        int sum=0;
        for(int k=0; k<z; k++)
        {
            sum+=ory[k];
        }
    return sum;
}

//Function that averages the array
double AverageArray(int gry[], int h)
{
    double sum = Total(gry, h);

    return sum/h;
}

//Function that averages the array
double AverageArray(double sum, int counter)
{
    return sum/counter;
}

//Function that determines the numbers that are larger than the average
int LargerThanAverage(int jry[], int i, double avg)
{
    int cnt=0;
    for(int j=0; j<i; j++)
    {
        if(jry[j]>avg)
        {
            cnt++;
        }
    }
    return cnt;
}

//Function that calculates the amount of numbers smaller than the average.
int SmallerThanAverage(int pry[], int i, double avg)
{
    int cnt = 0;
    for(int j=0; j<i; j++)
    {
        if(pry[j]<avg)
        {
            cnt++;
        }
    }
    return cnt;
}

//Function that converts negative integers to positive.
void NegativeToPositive(int ury[], int m)
{
    out << "\n\nSecond Array: Negative to Positive\n";
    for(int n=0; n<m; n++)
    {
        if(ury[n]<0)
        {
            ury[n] *= -1;
        }
    }
}

//Function that finds the largest value and its position
double LargestNumber(int ary[], int counter, int& position)
{
    int largest=ary[0];
    for(int i=0;i<counter;i++)
    {
        if(largest<ary[i])
        {
            largest=ary[i];
            position=i;
        }
    }
    return largest;
}

//Function that finds the smallest value and its position
double SmallestNumber(int ary[], int counter,int& position)
{
    int smallest=ary[0];
    for(int i=0;i<counter;i++)
    {
        if(smallest>ary[i])
        {
            smallest=ary[i];
            position=i;
        }
    }
    return smallest;
}

//Function that zeros out even values
void ZeroOutEvens(int ary[], int counter)
{
    out << "\n\nThird Array: Zero-out Even Values\n";
    for(int i=0;i<counter;i++)
    {
        if(ary[i]%2 == 0)
        {
            ary[i]=0;
        }
    }
}

OUTPUT
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
	Array Work-Out Program
First Array: 
24, 43, 63, -72, 73, 72, 77, 23, 36, 94, 
28, 93, 9, 92, -8, 83, -36, 73, 5, 38, 
56, 17, 93, 80, 99, 26, 19, 62, 72, 8, 
4, 48, 21, 54, 84, 32, 97, 74, 94, 82, 
57, 4, 38, 27, 83, 83, 22, 27, 64, 7, 
44, 26, 45, 66, 83, 62, 52, -7, -36, 62, 
78, -67, 34, 73, 93, 8, -3, -2, 2, 27, 
1, 11, 12, -73, 83, 77, 37, 72, 82, 23, 
Sum = 3309.00 COUNT = 80 AVERAGE = 41.36

Values LARGER than AVERAGE: 41
Values SMALLER than AVERAGE: 39

Second Array: Negative to Positive
24, 43, 63, 72, 73, 72, 77, 23, 36, 94, 
28, 93, 9, 92, 8, 83, 36, 73, 5, 38, 
56, 17, 93, 80, 99, 26, 19, 62, 72, 8, 
4, 48, 21, 54, 84, 32, 97, 74, 94, 82, 
57, 4, 38, 27, 83, 83, 22, 27, 64, 7, 
44, 26, 45, 66, 83, 62, 52, 7, 36, 62, 
78, 67, 34, 73, 93, 8, 3, 2, 2, 27, 
1, 11, 12, 73, 83, 77, 37, 72, 82, 23, 
Sum = 3917.00 COUNT = 80 AVERAGE = 48.96
Largest Number of the Array is: 99 Position: 24
Smallest Number of the Array is: 1 Position: 70

Third Array: Zero-out Even Values
0, 43, 63, 0, 73, 0, 77, 23, 0, 0, 
0, 93, 9, 0, 0, 83, 0, 73, 5, 0, 
0, 17, 93, 0, 99, 0, 19, 0, 0, 0, 
0, 0, 21, 0, 0, 0, 97, 0, 0, 0, 
57, 0, 0, 27, 83, 83, 0, 27, 0, 7, 
0, 0, 45, 0, 83, 0, 0, 7, 0, 0, 
0, 67, 0, 73, 93, 0, 3, 0, 0, 27, 
1, 11, 0, 73, 83, 77, 37, 0, 0, 23, 
Sum = 1875.00 COUNT = 80 AVERAGE = 23.44


Now I have another task that is new to me. I have a second infile of 5 numbers. I need to compare the "Third Array" with the first input from the second infile. I need assistance on how to set this up.
Is that how functions work? I can call it within my main and I can choose which parameter within the function call I want to use?

pos had the position because 1) it was a reference parameter -- that & on it makes it so, which means if function changes it, the original calling variable is overwritten. You tell the machine what to do, and it does it, if you get the logic and syntax right. Programming is a rule based system, so your job is to learn the rules.


with the prototypes is it necessary to place them in order of being called in main? Or is it ok to re-arrange?

No, you can't rearrange them. If they are the same time, you technically can, but assuming your code does anything remotely interesting to the parameters, this will cause something to go horribly wrong. This is why good names are critical.


Also, is it important at all to have main first above all other functions?

Nope. Main can go anywhere. Its actually usually in its own file in multi-file code which you are not doing yet. Save that for later. I put main at the bottom so I don't need prototypes in small little programs.

Andy mentioned before I shouldn't use improper names for functions and variables. In a function I'm aware that the variable is only good for that function. So names can be repeated. But is it wise to use the same name within various functions?

you can reuse a name in different functions. Just use common sense about it, and you will get a feel for it. In classes, parameters that match members causes you to have to add clarification to which one is which. If you ever have to add things to tell what name is which variable, you should rename one rather than clarify IMHO. There may be rare places where you can't do that, but usually, you can.
Last edited on
I've added this function into my code. I need to compare the new infile to the previous array of numbers(with zeros). Im just stuck on what I am doing with this.

This is all been added to the code
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
//Function reads the second file
void ReadParameters(int param[],int& counter)
{
    int num;
    counter=0;
    while(in2 >> num && counter < MAX)
    {
        param[counter] = num;
        counter++;
    }
}

//Function that prints the Parameters to the file
void PrintParameters(int par[], int& counter)
{

    for(int i=0; i<counter; i++)
    {
        out << par[i] << ", ";
        if((i+1)% 5 == 0)
        {
            out << endl;
        }
    }
}

//Function determines the values less than
int ParameterLessThan(int ary[], int counter)
{
    int values=;
    for(int i=0;i<counter;i++)
    {
        if(values<ary[0])
            values++;
    }
    return values;
}


I'm not confident on how to compare the two arrays with one another.
1
2
3
4
5
6
7
8
9
10
11
//Function for multiples of fourth
int MultipleOfFourth(int ary[],int bry[], int counter)
{
    int cnt=0;
    for(int i=0;i<MAX;i++)
    {
        if(i%ary[3]==0) 
            cnt++;
    }
    return cnt;
}


Does this code look correct to seasoned eyes? First time I compiled and ran it sput out a reasonable number, I just wasn't surrrre if this is how it's written.

EDIT: The above code only produces the numbers that are multiples of to MAX(100). I need it to compare ary[3] to the third array of numbers and select the multiplies within that array.

If I wanted to list out the numbers that are the multiples of 4 how would I approach that?
Last edited on
you pass in a parameter you do not use (counter).
and an array you do not use (bry).
why?

What is it you want to compute? You have a count of what numbers from 0 to 100 (why zero and 1 are considered, I do not know.. does that make math sense to you?!) can be divided evenly by ary[3].

the correct function to me would be
int Metc(int a)
{
blah blah
if(i%a ==0)
blah
}

and call it
Metc(ary[3]);

assuming it does what you want it to do, which is unclear to me.
I passed those in the function because I assumed they would be needed for the function to work.

I thought I'd need to compare both array's of numbers

I want the function to produce the numbers in the array that are multiples of 7 which is ary[3]. Currently, it produced the numbers that are multiples of 7 to 100. That's not what I need.


ary[3]=7 I need to compare every number in the third array(with the zeros) to 7(which are multiples of 7) and tally up the total. ary[3] is the new file of numbers and bry[i] is the previous array of numbers with the zeros.

I tried to do
1
2
3
4
5
6
7
8
for(int i=0;i<MAX;i++)
{
if(bry[i] % ary[3] == 0)
{ 
cnt++
out << i << ", ";
}
}


Last edited on
I attempted to do
1
2
3
4
5
6
7
8
 for(int i=0;i<bry[i];i++)
{
if(i % ary[3] == 0)
{ 
cnt++
out << i << ", ";
}
}

1
2
3
4
5
6
7
8
for(int i=0;i<counter;i++)
{
if(bry[i] % ary[3] == 0)
{ 
cnt++
out << i << ", ";
}
}


I end up getting garbage or multiples of literal 7 instead of being multiples of 7 within the array
The second block of code above looks right, assuming counter is size of bry array. But its writing out i, not bry[i], so I think you need to make that small change.
Topic archived. No new replies allowed.
Pages: 12