Dumbass calculator using pointer doesn't work

I am making a calculator where I will input 5 array of numbers and calculate those numbers with pointers or whatever you call it.

I am getting an error like

note C:\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:63 candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

and more error if I try to fix the code. So yeah, can i have a hand?

Btw, here's my dumbass 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
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
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
 
// function declaration:
void getAverage(int*);
void getSum(int*);
void getDifference(int*);
void getProduct(int*);

main ()
{
   // an int array with 5 elements.
   int balance[5];

for(int x=0; x<5; x++){
        cin>>balance[x];
        
        } 
   // pass pointer to the array as an argument.
  
   // output the returned value 
   cout << "Product: " << getProduct(balance) <<endl;
   cout << "Difference value is: " << getDifference(balance)<< endl; 
   cout << "Sum value is: " <<  getSum(balance) << endl;    
   cout << "Average value is: " << getAverage(balance)<< endl; 
    
getch();
}

void getAverage(int* arr)
{
  int    i, sum = 0;       
  double avg;          
 
  for (i = 0; i < 5; ++i)
  {
    sum += arr[i];
   }
 
  avg = double(sum) / 5;
 

}

void getSum(int* arr)
{
    int sum=0; 
  for (int i = 0; i < 5; ++i)
  {
    sum += arr[i];
   }

    
    }
void getDifference(int* arr)
{
 
 int diff=0;
   for (int i = 0; i < 5; ++i)
  {
    diff -=arr[i];
   }
     

}

void getProduct(int* arr)
{
 
 int prod=1;
   for (int i = 0; i < 5; ++i)
  {
    prod *=arr[i];
   }
    

}


just starting w/ c++
i cri
Last edited on
You try to print the value that is returned from the functions but they don't return anything, because they have return type void.
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
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
 
// function declaration:
double getAverage(int*);
int getSum(int*);
int getDifference(int*);
int getProduct(int*);

main ()
{
   // an int array with 5 elements.
   int balance[5];

for(int x=0; x<5; x++){
        cin>>balance[x];
        
        } 
   // pass pointer to the array as an argument.
  
   // output the returned value 
   cout << "Product: " << getProduct(balance) <<endl;
   cout << "Difference value is: " << getDifference(balance)<< endl; 
   cout << "Sum value is: " <<  getSum(balance) << endl;    
   cout << "Average value is: " << getAverage(balance)<< endl; 
    
getch();
}

double getAverage(int* arr)
{
  int    i, sum = 0;       
  double avg;          
 
  for (i = 0; i < 5; ++i)
  {
    sum += arr[i];
   }
 
  avg = double(sum) / 5;
 
return avg;
}

int getSum(int* arr)
{
    int sum=0; 
  for (int i = 0; i < 5; ++i)
  {
    sum += arr[i];
   }

    return sum; 
    }
int getDifference(int* arr)
{
 
 int diff=0;
   for (int i = 0; i < 5; ++i)
  {
    diff -=arr[i];
   }
     
return diff; 
}

int getProduct(int* arr)
{
 
 int prod=1;
   for (int i = 0; i < 5; ++i)
  {
    prod *=arr[i];
   }
    
return prod; 
}



I think this should work BUT I have another problem. The product and difference doesn't seem to print the correct numbers. I can't think of the logic.
Last edited on
Don't get frustrated. This is actually pretty good beginner code.

What are the inputs you're giving and the output for getProduct(). You might have a problem if the inputs are large because the product could overflow the size of an int. This is likely only if the product is over 2 billion.

By the way, note that getAverage() could be computed with the help of getSum():
1
2
3
int getAverage(int *arr) {
    return (double)getSum(arr) / 5;
}

The difference doesn't seem to print the correct numbers though. Instead of subtracting the numbers, the program adds the numbers up and prints it with a negative (-) sign.

Like if I subtract 12,1,1,1,1 it prints -16.
The difference doesn't seem to print the correct numbers though. Instead of subtracting the numbers, the program adds the numbers up and prints it with a negative (-) sign.

Like if I subtract 12,1,1,1,1 it prints -16.


What do you expect to happen, and what it supposed to be a difference of an array? Are you sure you're not supposed to calculate the difference of two arrays?
Last edited on
I mean the answer should be 8, right? Or i'm getting this wrong?
Is this a homework assignment? If so, then please re-read the assignment carefully to see what it says about the difference. This is an odd operation.

If the difference should be "first element minus all the other elements" then you need to change your getDifference() function. Initialize diff to the value of the first element. Then run the for loop from the 2nd to 5th elements.
Topic archived. No new replies allowed.