learning arrays

Hi, i am learning the arrays and function at the moment and writing a program to calculate the average and standard deviation that reads up to 100 ints from file to array.

The EXIT(1) in the main doesn't work and the compiler says that i haven't declare the exit.. But it works as the cin right? so i don't have to declare it. Please somebody look at it.

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
#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
using namespace std;

double calc_average(const int value[], const int COUNT);

double calc_std(const int value[], int COUNT, double average);


const int array_size = 100;

int main (){

  ifstream input;
  double average, std;
  int value[array_size], COUNT = 0;

  cout.setf(ios::showpoint);
  cout.setf(ios::fixed);
  cout.precision(2);

  input.open("ints-1-col.dat");
  if (input.fail())
    {
      cout<<"Input file opening failed"<<endl;
      exit(1);
    }

  while (input>>value[COUNT])
    COUNT++;

  average = calc_average(value, COUNT);
  std = calc_std(value, COUNT, average);

  cout<<COUNT<< " values for average = "<<average<< " and for starndard deviation = " <<std<<endl;

  input.close();
  return 0;

}

double cal_average(const int value[], const int COUNT)
{

  int i;
  int sum = 0;
  double average;

  for (i=0; i<COUNT; i++)
    sum = sum + value [i];

  average = static_cast<double>(sum)/static_cast<double>(COUNT);

  return (average);
}

double calc_std(const int value[], int COUNT, double average)
{
  int e;
  double final_std, sum_x_squares, add_sum_x_squares;

  for (e = 0; e<COUNT; e++){
  sum_x_squares = (value[e] - average)*(value[e] - average);
  add_sum_x_squares = sum_x_squares + add_sum_x_squares;
}

final_std = sqrt(add_sum_x_squares/static_cast<double>(COUNT));

 return (final_std);
}
Try #include <cstdlib> or just return 1; instead of exit(1);
Thank you, just did. BUT there is another problem caught by the compiler on line 34. it says that undefined reference to 'calc_average(int const*, int)'. What does it mean? Did i forgot to declare something or it's invalid to declare two const in the function call?
The linker can't find the definition of calc_average because you misspelled it.
LOL. Thank you! Although it compiled, the program doesn't run... omg this course is hard >.<''!

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
#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
#include <cstdlib>

using namespace std;

double calc_average(const int value[], const int COUNT);

double calc_std(const int value[], int COUNT, double average);


const int array_size = 100;

int main (){

  ifstream input;
  double average, std;
  int value[array_size], COUNT = 0;

  cout.setf(ios::showpoint);
  cout.setf(ios::fixed);
  cout.precision(2);

  input.open("ints-1-col.dat");
  if (input.fail())
    {
      cout<<"Input file opening failed"<<endl;
      exit(1);
    }

  while (input>>value[COUNT])
    COUNT++;

  average = calc_average(value, COUNT);
  std = calc_std(value, COUNT, average);

  cout<<COUNT<< " values for average = "<<average<< " and for standard deviation = " <<std<<endl;

  input.close();
  return (0);

}

double calc_average(const int value[], const int COUNT)
{

  int i;
  int sum = 0;
  double average;

  for (i=0; i<COUNT; i++)
    sum = sum + value [i];

  average = static_cast<double>(sum)/static_cast<double>(COUNT);

  return (average);
}

double calc_std(const int value[], int COUNT, double average)
{
  int e;
  double final_std, sum_x_squares, add_sum_x_squares;

  for (e = 0; e<COUNT; e++){
  sum_x_squares = (value[e] - average)*(value[e] - average);
  add_sum_x_squares = sum_x_squares + add_sum_x_squares;
}

final_std = sqrt(add_sum_x_squares/static_cast<double>(COUNT));

 return (final_std);
}
NO, IT JUST RUN! I misspelled again!!! LOL, hey Thank you for helping me :) . I'M HAPPY! 2 more project homework to go!
Topic archived. No new replies allowed.