completely overwhelmed

So. The goal is to populate an array with sixteen numbers from a file. Take those numbers and perform median, average, standard deviation and variance. I'm loathe to ask for help but I've been beating my head against the wall for two days and I'm desperate. I know it's most likely a mistake in syntax or headers or something so completely obvious, but I can't seem to lock it down. Been tearing up the internet trying to find a solution but mostly I'm just deepening the confusion. Would appreciate any links (kinda like to figure it out myself) but at this point I'll take whatever help I can get. Brains fried.

The problems:
- "undefined" errors on "ifstream" "myfile" and the top bracket
- "expected declaration" on the "if" and "else" of the if/else/while loop
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

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <cmath>

// declare array
int numbers[16];

// declare functions
int median, average, standard_deviation, variance, sum, med, sta, var, ave;

// declare variables 
int count;
	 
}	
int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Processing 16 numbers" << endl;
	
// store the numbers in an array (n an integer variable) double or float
	
	
	ifstream myfile;
			
	myfile.open("C:\Documents\school work\programing");
			
	int numbers[16];
				
	if (!myfile);
				{	
	cout << "File aint here man.  Start over!" << endl;
				}
	else 
						{
				
	cout << "I've got your file right here man." << endl;
	count=0;
	while (!myfile.eof()) 
						{
	myfile >> numbers[count];
	count++;
						}



Then it gets a little wierd....

- "no storage class or type specifier" errors on all the cout's and variables and the "system" on system pause. (this has got to be with the brackets [right?]but I cant figure it out)
- "+=" after sum "expected a ';'"
- and each "<<" operator and the "return" of return 0 expected a declaration.
-and the "sqrt" has the error "more than one instance of overloaded function "sqrt" matches the argument list" *head desk*

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

// create function to calculate the average of numbers in array
	sum += numbers[16];
		
	ave = sum/16;
	
	// output average
	
	cout << "The average is: " << ave << endl;
	
	// creat function to calculate median of numbers in array
	
	med = (numbers[8] + numbers[9])/2;
	
	// output median
	
	cout << "The median is: " << med << endl;

	// create function to calculate the variance of numbers in array
	
	var = (numbers[16]-ave^2);
	
	// output variance 
	
	cout << "The variance is: " << var << endl;
	
	// creat function to calculate standard deviation of numbers in array
	
	sta = sqrt(var);
	
	// output standard deviation
	
	cout << "The standard deviation is: " << sta << endl;
	
	
	

	system("pause");
	return 0;
}	




I'm taking an intro to coding, supposed to be strictly pseudocode but all we've done is C++. I don't know if that's normal but I'm lost. Thanks for any halp. I very much appreciate it. -Heavydrop
Last edited on
A few things I can spot:

1) Stray closing bracket right before main.

2) Semicolon after if (!myfile).

3) The ^ operator isn't doing what you think - it's not a square operator, it's a bitwise XOR.

4) The sqrt function doesn't take an integer. You need to cast it to something it recognises or pass a different type, such as a double or float, into it. Otherwise the compiler will make a best guess as to what you're trying to achieve.

5) For arrays, you need to create a loop to access elements for operations like sums. The line sum += numbers[16] is trying to access a single element of the array (17, arrays start at zero). In addition to that, element 17 doesn't exist - you created an array with a size of 16, thus the valid elements are 0 - 15.

6) You have a comment that says declaring functions - you're actually just declaring integers there. This site has some excellent beginner function tutorials which might help: http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
1) got it...

2) Removed it....

3) would it be better to express it like this?
 
var = (numbers[17] - ave)(numbers[17] -ave)


4) Changed the "var" to float, would I need to change anything else to float?

5) Ack, rookie mistake. Would the loop just be
 
for (numbers=0; numbers <5; numbers++)

??

6) would the function commands be set up like this?
 
int sum, med, sta, ave, var (numbers[17])


I've looked at the site with functions but I'm not really understanding how they work with the arrays
1) Good. :-)

2) Good. :-)

3) C++ has a function for squaring. You'll need to include the cmath header. First parameter is the base, second is the exponent. Here's a quick example:
1
2
3
4
5
6
7
8
#include <iostream>
#include <cmath>

int main( int argc, char* argv[] )
{
  std::cout << std::pow( 8, 2 ) << std::endl;
  return 0;
}


4) Probably most things. Average and the like, at least. Even if your array stays as a list of integers, it's not likely the average will be. For example, 1 and 2 are both integers but the average of them in a list would be 1.5. You need floats or doubles to handle that precision.

5) Not quite. You're already using the identifier 'numbers'. Standard convention for loop counters is an arbitrary letter, usually i or j. Again, quick example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main( int argc, char* argv[] )
{
  int myArray[] = { 1, 2 };
  int sum = 0;

  for( int i=0; i < 2; ++i )
  {
    sum += myArray[i];
  }

  std::cout << "Sum is " << sum << std::endl;
  std::cout << "Average is " << sum / 2.0 << std::endl;
}


6) The function syntax is
[return type][function name]( [arguments] )
{
   Function Body
}

One thing with arrays is that they're inherently passed as references. So anything you do inside of the function will affect the array passed in. By default, most data types are passed by value, meaning a copy is created for use within the function and the original isn't altered. Run the following example to see what I mean.
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
#include <iostream>

void AddToValue( int a )
{
  a += 5;
}

void AddToArrayValue( int a[], int size )
{
  for( int i=0; i < size; ++i )
  {
    a[i] += 5;
  }
}

int main( int argc, char* argv[] )
{
  int myInt = 2;
  int myArray[] = { 1, 2 };

  AddToValue( myInt );
  AddToArrayValue( myArray, 2 );

  std::cout << "myInt: " << myInt << std::endl;

  std::cout << "myArray: ";
  for( int i=0; i < 2; ++i )
  {
    std::cout << myArray[i] << " ";
  }
}

Last edited on
This is frustrating lol, I'm getting what you're saying, even understanding the code a little bit but it's like I'm reading it in Spanish and the context isn't clicking. I'm trying to rework it with the examples you gave me........
Which bits aren't you understanding?
All of it. It makes sense. Sort of.
This whole class has been an 8 week cannon-ball run through code that has been barely explained, we're not even supposed to be learning code yet lol. My last assignment was displaying a multiplication table. The site helped ALOT with that but this seems to be a whole other animal. Maybe I'm just over intimidated or whatever but my mind is clay with this damn code. I understand the pseudocode, sort of. But we spent one class on that and then he threw us out of the nest and I've been clawing my way through it like mad man. Now I guess I'm sliding back down hill?
For example, with the function syntax:

[return type][function name][arguments]
{
function body
}

what is the return type? the function, I'm assuming is the variance, sum, median, et cetera. but the arguments, what is that in relevance to this code? I'm trying to figure it out but it's not clicking.
what is the return type? the function, I'm assuming is the variance, sum, median, et cetera. but the arguments, what is that in relevance to this code? I'm trying to figure it out but it's not clicking.


The return type is the type of the value returned by the function.
The arguments are the values fed to the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 -- return type.
|      -- function name
|     |       -- parameter type/name
|     |      |
|     |      |
V     V      V
int add( int a, int b)
{
    return a+b ;
}

                      -- argument
int main()           |    -- argument
{                    |   |    
                     V   V 
      int num = add( 3,  5 ) ;
}


The value returned by the add function is assigned to the variable num.
okay, so something like this?
1
2
3
int average (numbers)
{
         return ??? 


not understanding how this works with the array? how do you translate the arguments from the array? how do you do it with the parameter?
I understand the correlation between int a, int b with the return a+b but not in correlation with the array.

1
2
3
4
5
6
7
int add (numbers)
{
        return (numbers)

int main ()
{
             int num = add(numbers) 


??
Last edited on
I understand the correlation between int a, int b with the return a+b but not in correlation with the array.


Keep in mind the definition/prototype will include the type in the list of parameters. It isn't necessary when feeding arguments to the function from somewhere else.

In the following case, there is no reason to return anything, because we are operating on the values in the array and since arrays are passed as pointers to the first element, the changes we make to elements of an array within a function will persist outside the function.


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
#include <iostream>

// This function takes an array of array_size elements and adds value to each element.
// prototypes, these are all equivalent:
void add( int array[], unsigned array_size, int value );
void add( int* array, unsigned array_size, int value );
void add( int array[5], unsigned array_size, int value ) ;
void add( int array[10], unsigned array_size, int value ) ;


// Add value to each element of array.
void add(int array[], unsigned array_size, int value )
{
    for ( unsigned i=0; i<array_size; ++i )
        array[i] += value ;
}

// Print each element of array.
void print( int* array, unsigned array_size )
{
    std::cout << "{ " ;
    for ( unsigned i=0; i<array_size; ++i )
        std::cout << array[i] << ' ' ;
    std::cout << "}\n" ;
}

int main()
{
    const unsigned aSize =5;
    int array[aSize] = { 1, 2, 3, 4, 5 } ;

    print(array, aSize) ;
    add(array, aSize, 10) ;
    print(array, aSize) ;
}

Shouldn't:
myfile.open("C:\Documents\school work\programing");


be:

myfile.open("C:\\Documents\\school work\\programing");


?
Topic archived. No new replies allowed.