Problems with methods and arrays

I am using Dev-C++ and MS Visual C++. I get a bunch of errors when I try to compile. Most I think are with the method calling\display.

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
//AS-31
#include <iostream>
#include <string>
using namespace std;

double computemin ( double sortedData [], int length );
double computemax ( double sortedData [ ], int length );
double computemean ( double sortedData [ ], int length );
double computemedian ( double sortedData [ ], int length);

void main ()
{
  double data [20],sdata[20];
  int length,i;
  double min,max,mean,median,sum,temp;
  bool swapped;
  //input data into array
  cout <<"Enter data size: "<<endl;
  cin >>length;
  for (i=0; i<length;i++)
  {
    cout <<"Enter a data item: ";
    cin >>data[i];
  }
    cout <<endl;
  //copy the contents of data into sdata

    for (i=0; i<length;i++)
  {
    sdata[i] = data[i];
  }
  //sort array sdata
  swapped=true;
  while (swapped)
    {
    swapped=false;
    for (i=0;i<length-1;i++)
    {
      if (sdata[i] > sdata[i+1])
     {
      //swap
      temp=sdata[i];
      sdata[i] = sdata [i+1];
      sdata [i+1] = temp;
      swapped=true;
      }
    }
  }
  //display original data
  cout <<"Original data: "<<endl;
  for (i=0; i<length;i++)
  {
    cout <<data[i] << " ";
  }
  cout <<endl;
  //display sorted data
  cout <<"Sorted data: "<<endl;
  for (i=0; i<length;i++)
  {
    cout <<sdata[i] << " ";
  }

  computemin (sdata [ ],length);
  computemax (sdata [ ],length);
  computemean (sdata [ ],length);
  computemedian (sdata [ ],length);
  
system ("PAUSE");
}

double computemin ( double sortedData [], int length )
{
  int min=sortedData[0];
  cout <<"Min: "<<min<<endl;
}

double computemax ( double sortedData [ ], int length )
{
   int max=sortedData[length-1];
    cout <<"Max: "<<max<<endl;

}

double computemean ( double sortedData [ ], int length )
{
  int sum=0;
  for (int i=0; i<length;i++)
  {
     sum=sum+sortedData[i];
  }
 int mean=sum/length; 
  cout <<"Mean: "<<mean<<endl;
}

double computemedian ( double sortedData [ ], int length)
{
  int indexHi,indexLo,index;
  if ((length %2) != 0 )
  {
    index = length / 2;
   int median = sdata [index];
  }
  else 
  {
   indexHi = length / 2;
   indexLo = indexHi -1;
   median = (sdata[indexLo] + sdata[indexHi] ) / 2;
  }
   cout <<"Median: "<<median<<endl;
}
Well if you work through the errors it's pretty easily fixed. So heres what I found.

1.)12 'main' must return 'int'
Pretty self explanatory, change it to int main and make it return 0.

2.)63-66 expected primary-expression before ']' token
Not so obvious.

1
2
3
4
5
ouble computemin ( double sortedData [], int length )
{
  int min=sortedData[0];
  cout <<"Min: "<<min<<endl;
}


If you have the prototype taking an array. You only put the name of the array (not the []) when you call the function.

computemin (sdata, length);

3.)sdata undeclared in computmedian prototype.
Use sortedData[], sdata is out of scope.
4.)median undeclared
You didn't declare it.
int median

You also get warnings of 'converting to int from double'. This isn't good because it isn't rounding the decimals. It chops them off. Meaning 3.999 as an int equals 3. Probably not what you want.

Another thing. All of your functions are of type double. That is the return type. Meaning it will return a double. Your functions don't return anything so they are void.

You have to learn how to use the compiler errors to fix your code. 3 of those errors were obvious and you should have been able to fix them yourself. And for next time, don't just say it has errors, fix it. Look at the errors, try to fix them yourself and if that doesn't work: Post your code, the exact wording of the error, what you have tried, and why you think it doesn't work.
Last edited on
The truth is that you have many errors and warnings.
To solve the errors:
1. (Lines 63-66): When you call a function, in order to pass it an array you just write the name of the array, without brakets"[". So, for example, line 63 should be computemin (sdata ,length);

2. All the functions are declared as double. That means that you have to have a return statement in each of the. You can put a return after each cout in every function.

3. In the function "computemedian()" you declare the value "median" in the if. This mean that this value is gonna be local to the if and when you exit the brackets the value is gonna be destroyed. In order to use it after the closing brackets of the if you have to declare it outside.
Also in the same function instead of using "sortedData" as the name of the array you use "sdata".

4. Your computations in your functions are all int. Which is a possible loss of data because your array is double. So if your max number is 14.5 you automatically cast it to int which will be 14, and you have wrong results....

Hope this helps.

[EDIT] I just saw the other reply....
To add something on what mikeb570 saied, try to see all the warnings in the code and correct them. The program will still run but it is not good to have warnings.

You don't have to change the void main() Your main function can be void also if you don't want it to return a value...
Last edited on
Strange, no matter what i do. (with no return at all and I tried return;) still says 'main' must return 'int'. Using Dev-C++. [shrugs]

Sorry bout beatin ya to it >.<

Edit: Wow, I typo a lot =/
Last edited on
Hmmm,
Then maybe it is just a VC++ acception that you can use void also.
I have seen it in some books and I assumed that it is normal.
As I read now it is not part of the standards. It has to be "int" type and return a value.
closed account (z05DSL3A)
From ISO/IEC 14882 (Second edition 2003-10-15)
3.6.1 Main function 1 A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [Note: in a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. ]

2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation-defined. All implementations
shall allow both of the following definitions of main:

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

In the latter form argc shall be the number of arguments passed to the program from the environment in
which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through
argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs)
(17.3.2.1.3.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the
name used to invoke the program or "". The value of argc shall be nonnegative. The value of
argv[argc] shall be 0. [Note: it is recommended that any further (optional) parameters be added after
argv. ]


Mitsakos wrote:

Then maybe it is just a VC++ acception that you can use void also.

VC uses alot of Language Extensions, if you disable these in your build it will complain about 'main' should be 'int' instead of 'void'
Last edited on
Topic archived. No new replies allowed.