Operations Program, Need guidance

I have a program I need to code that: Handles 25 interger numbers. Declares Array A and B.

Fill Array A from input file "numbers2.in".
Fill Array B with 25 random numbers from 1-100.(no zero)
Print both arrays with labels(across not down page). Define one function that prints only one array.
Write a function that will Add array A with B and put results into C and print to C.
Write a function that will multiply A with B and put results into C and print to C.
Find the Average for Array A and B. Print both avg with labels.
Find the largest value in each Array. Print both.
Find the smallest value in each array. Print both.
Find the average of the 'odd' numbers in each array. print both.

I assumed this program would be easy enough to follow and I tried my best. I just feel so confused. This isn't a complete code. I'm only posting this for more eyes to view my mistakes and help me understand how to correct them.


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
#include <iostream>
#include <fstream>
using namespace std;
ifstream inF;
int ArrayA(ofstream& outF, ifstream& inF, int x, int n)
{
	inF.open("numbers2.in");
	for(x=0;x<n;x++)
		inF>>ArrayA[x];
	return ArrayA[];
}
int ArrayB(ofstream& outF,int x)
{
	for(x=0;x<n;x++)
	ArrayB[25]=rand()%100+1;
	out<<ArrayB[x];
	return ArrayB[];
}
void printArray(int arr[], int n, ofstream& outF)
{
    int x=0;
    for(x=0;x<n;x++)
		out<<arr[x]<<" \n";
}
void Add(int a[], int b[], int c[], int n, ofstream& outF)
{
	int x=0;
	for(x=0;x<n;x++)
		c[x]=a[x]+b[x];
}
void Product(int a[], int b[], int c[], int n, ofstream& outF)
{
	int x=0;
	for(x=0;x<n;x++)
		c[x]=a[x]*b[x];
}
void Average(int a[], int n, ofstream& outF)
{
	float sum=0;
	int x=0;
	for(x=0;x<n;x++)
		sum+=a[x];
	out<<(sum*1.0)/n<<" \n";
}
void Largest(int arr[], int n, ofstream& outF)
{
	int max=-1, x=0;
	for(x=0;x<n;x++)
		if(arr[x]>max)
			max=arr[x];
		out<<max<<" \n";
}
void Smallest(int arr[], int n, ofstream& outF)
{
	int min=999, x=0;
	for(x=0;x<n;x++)
		if(arr[x]<min)
			min=arr[x];
		out<<min<<" \n";
}
void Avg_Odd(int arr[], int n, ofstream& outF)
{
	int count=0, sum=0, x=0;
	for(x=0;x<n;x++)
		if(arr[x]%2==1)
			sum+=arr[x];
			count++;
		out<<(sum*1.0)/count<<" \n";
}
int main()
{
    ofstream outF;
    outF.open("operation.out");
	int A[25], B[25], C[25], x=0;
out<<"array A has :\n ";printArray(A,25);
out<<"array B has :\n ";printArray(B,25);
Add(A,B,C,25);
out<<"array C when A,B are added:\n";printArray(C,25);
Product(A,B,C,25);
out<<"array C when A,B are Multiplied:\n";printArray(C,25);
out<<"Average of array A is:";Average(A,25);
out<<"Average of array B is:";Average(B,25);
out<<"largest number in array A is:";Largest(A,25);
out<<"largest number in array B is:";Largest(B,25);
out<<"smallest number in array A is:";Smallest(A,25);
out<<"smallest number in array B is:";Smallest(B,25);
out<<"Average of odd numbers in array A is:";Avg_Odd(A,25);
out<<"Average of odd numbers in array B is: ";Avg_Odd(B,25);
out.close();
in.close();
return 0;
}
Last edited on
What is your question?
No specific question stated. I just needed/wanted someone to look over my code and see if there are any or a lot of mistakes. Things like the first function to read in the numbers from an infile, I feel is wrong and needs to be adjusted. I'm unsure how.

Now that I'm looking, line 47 and 55 why would I put max=-1 and min=999? seems it owuld be opposite?!

And I keep getting an error on line 9. Saying no match for operator>>
Last edited on
Did you copy the code from a source you didn't understand? If not, you should be able to tell us why the initial value of max is -1. When you are looping through numbers, you are checking if the current number is larger than what the current "max" is, and if so, assigning it to be the new max number. So logically, the initial value of max should either be the first element of the array, or should be a value so small such that every number in the array is greater than it, initially.

And I keep getting an error on line 9. Saying no match for operator>>

This is the same issue as the other thread. You're trying to read a value into the name of the function. You need to pass an array to your function like you do in your printArray function.

Yes and no. I did copy the code, but i'm trying to manipulate it to meet my needs. I'm very confused and spent hours trying to figure out my errors. I can't get the first two functions to even work properly. Having a difficult time tying to figure out how to read in the infile numbers for Array A, and having an even more difficult time figuring out the random numbers for Array B.

I apprciate the responses, but can you please show me in code, i learn better by seeing than trying to decipher whats needed based off words.
I've successfully altered some of the code and its now running. Issue I've run into now is getting the print outs to be as they need to be. for example, in the part when i need to add a[]+b[]=c[], It gives me huge numbers. What am I doing wrong?

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

//Eighth Programs
//Operations Program
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
ofstream outF;
ifstream inF;

const int n=25;
int ArrayA(ofstream& outF, ifstream& inF)
{
    inF.open("numbers2.in");
    int A[n],x;
    for(x=0;x<n;x++)
    {
        inF>>A[x];
        outF<<A[x]<<endl;
    }
}
int ArrayB(ofstream& outF)
{
    int B[n],x;
	for(x=0;x<n ;x++)
  {
	B[x]=rand()%100+1;
	outF<<B[x]<<endl;
  }
}
void printArray(int arr[], ofstream& outF)
{
    int x=0;
    for(x=0;x<n;x++)
    outF<<arr[x]<<" \n";
}
void Add(int a[], int b[], int c[], ofstream& outF)
{
    ArrayA(outF,inF);
    ArrayB(outF);
	int x=0;
	for(x=0;x<n;x++)
    c[x]=a[x]+b[x];
    outF<<c[x]<<endl;
}
void Product(int a[], int b[], int c[], ofstream& outF)
{
	int x=0;
	for(x=0;x<n;x++)
    c[x]=a[x]*b[x];
}
void Average(int a[], ofstream& outF)
{
	float sum=0;
	int x=0;
	for(x=0;x<n;x++)
		sum+=a[x];
	outF<<(sum*1.0)/n<<" \n";
}
void Largest(int arr[], ofstream& outF)
{
	int max=0, x=0;
	for(x=0;x<n;x++)
		if(arr[x]>max)
			max=arr[x];
		outF<<max<<" \n";
}
void Smallest(int arr[], ofstream& outF)
{
	int min=999, x=0;
	for(x=0;x<n;x++)
		if(arr[x]<min)
			min=arr[x];
		outF<<min<<" \n";
}
void Avg_Odd(int arr[], ofstream& outF)
{
	int count=0, sum=0, x=0;
	for(x=0;x<n;x++)
		if(arr[x]%2==1)
			sum+=arr[x];
			count++;
		outF<<(sum*1.0)/count<<" \n";
}
int main()
{
    outF.open("operation.out");
	int a[n], b[n], c[n], x=0;
outF<<"array A has :\n ";ArrayA(outF,inF);
outF<<"array B has :\n ";ArrayB(outF);
outF<<"array C when A,B are added:\n";ArrayA(outF,inF);Add(a,b,c,outF);
outF<<"array C when A,B are Multiplied:\n";Product(a,b,c,outF);
outF<<"Average of array A is:";Average(a,outF);
outF<<"Average of array B is:";Average(b,outF);
outF<<"largest number in array A is:";Largest(a,outF);
outF<<"largest number in array B is:";Largest(b,outF);
outF<<"smallest number in array A is:";Smallest(a,outF);
outF<<"smallest number in array B is:";Smallest(b,outF);
outF<<"Average of odd numbers in array A is:";Avg_Odd(a,outF);
outF<<"Average of odd numbers in array B is: ";Avg_Odd(b,outF);
outF.close();
inF.close();
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
void Add( int a[], int b[], int c[], ofstream& outF )
{
    ArrayA(outF,inF); // this does not modify a, b, or c
    ArrayB(outF); // this does not modify a, b, or c
    int x=0;
    for ( x=0; x<n; x++ )
      c[x] = a[x] + b[x]; // this is the only statement inside the loop

    outF <<c[x] << endl; // this accesses c[n]. The c does not have n+1 elements.
}


1
2
int a[n], b[n], c[n]; // uninitialized arrays
Add( a, b, c, outF );

The values of elements of a and b were never set.
Therefore, the values to set to elements of c in Add() are unknown.
(Then again, you never output any of those elements.)


Why do you have function 'printArray()' that you never use?
please correct me if I'm wrong but

shouldn't passing an array like int a[] as a functions argument be illegal? at least it's illegal in C++98,

the correct syntax would be

1
2
 void Add( int* a, int* b, int* c, ofstream& outF )
 


as arrays decay into pointers, right??
Last edited on
> shouldn't passing an array like int a[] as a functions argument be illegal?
> at least it's illegal in C++98,

Syntactically, the function parameter int a[] or int b[23] has always been valid.
The apparent type of such a parameter is (silently) replaced by 'pointer to int'
(int* a or int* b in the above example).

The type of each function parameter in the parameter list is determined according to the following rules:

1) First, decl-specifier-seq and the declarator are combined as in any declaration to determine the type.
2) If the type is "array of T" or "array of unknown bound of T", it is replaced by the type "pointer to T"
3) If the type is a function type F, it is replaced by the type "pointer to F"
4) Top-level cv-qualifiers are dropped from the parameter type (...)

https://en.cppreference.com/w/cpp/language/function#Parameter_list


1
2
3
4
5
6
7
8
9
#include <iostream>
#include <typeinfo>

void foo( int a[], int b[23] ) ;

int main()
{
    std::cout << "type of the function foo is: " << typeid(foo).name() << '\n' ;
}

echo && g++ -std=c++17 -O3 -march=native -Wall -Wextra -pedantic-errors -Wno-sign-compare main.cpp && ./a.out | c++filt -t

type of the function foo is: void (int*, int*)

http://coliru.stacked-crooked.com/a/c3de1ffd8ffd162f
Last edited on
@CodeNovice01:
Read http://www.cplusplus.com/doc/tutorial/functions/ and http://www.cplusplus.com/doc/tutorial/arrays/

(I'd expect your teacher to provide such material, but if you really are on your own and don't know where to look.)
Topic archived. No new replies allowed.