Why is the value showing as 0 in my function?

Isn't it written correctly?

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
#include <stdio.h>
#include <stdlib.h>

void Instruct( void );
void GetNumbers( int [] );
int FindLargest (int []);

void main()
{
	int a[ 10 ];

	Instruct();
	GetNumbers( a );

	printf("the largest value is %d\n", FindLargest(a));
	
	
	system( "pause" );
}

void Instruct( void ) 
{
	printf( "This program will determine the largest and smallest values in a set of ten numbers. "
	"It will also compute the sum and average of the numbers.\n\n" );
}

void GetNumbers( int [] )
{
	int b[ 10 ];
	int number = 0;
	
	printf( "Please enter your list of ten numbers: " );

	for ( int i = 0 ; i < 10; i++ ) {
		scanf( "%d", &number );
		b[ i ] = number;
	}

	printf( "Your array has these values: " );

	for ( int i = 0 ; i < 10; i++ ) {
		printf( "%d ", b[ i ] );
	}

	printf( "\n" );
}

int FindLargest ( int array [] )

{
	
	int maxNumber = 0 ;
	for(int i = 0; i < 10; i++)
	{
		if(array[i] > maxNumber)
	maxNumber = array[i];
	}
	return maxNumber;
	}
The problem is with your GetNumbers. Do you see what happens there?

-Albatross
I don't see it, my GetNumbers function returns the values that the user enters, and that is what I want to happen.

Are you referring to something else?
No, it doesn't. Compare the headers for GetNumbers() and FindLargest().

BTW, I would recommend passing the size of the array as an additional argument, instead of assuming it will always be ten.

[edit] fixed typo
Last edited on
Actually, Duoas brings up a good point. Don't assume the array size will be 10; it might be a good idea to pass the array size as an extra variable. But! Get your program to work for a size 10 array first. :)

Line 27: Are we missing something here?
Line 29: Do we have something redundant here?

-Albatross
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void GetNumbers( int b[] )
{
	int number = 0;
	
	printf( "Please enter your list of ten numbers: " );

	for ( int i = 0 ; i < 10; i++ ) {
		scanf( "%d", &number );
		b[ i ] = number;
	}

	printf( "Your array has these values: " );

	for ( int i = 0 ; i < 10; i++ ) {
		printf( "%d ", b[ i ] );
	}

	printf( "\n" );
}

this function may be you want;
ok, so I fixed what you suggested.

I have modified the FindLargest function and now I get a "-76387648", type number (which I assume is the memory location)

My GetNumbers function still works the same, should there have been a change?

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
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
void Instruct( void );
void GetNumbers( int [], int size);
int FindLargest (int []);

void main()
{
	int a[ SIZE ];

	Instruct();
	GetNumbers( a , SIZE);

	FindLargest(a);
	//printf("%d",FindMax(array));
	
	system( "pause" );
}

void Instruct( void ) 
{
	printf( "This program will determine the largest and smallest values in a set of ten numbers. "
	"It will also compute the sum and average of the numbers.\n\n" );
}

void GetNumbers( int array [], int size )
{
	int b[ SIZE ];
	int number = 0;
	
	printf( "Please enter your list of ten numbers: " );

	for ( int i = 0 ; i < size; i++ ) {
		scanf( "%d", &number );
		b[ i ] = number;
	}

	printf( "Your array has these values: " );

	for ( int i = 0 ; i < size; i++ ) {
		printf( "%d ", b[ i ] );
	}

	printf( "\n" );
}

int FindLargest ( int array [] )

{
	int maxNumber;
	int temp = array[0] ;

	printf("the largest value is");
	for(int i = 0; i < 10; i++)
	{
		if(array[i] > temp)
	maxNumber = array[i];
	}
	return printf("%d", temp);
	}
Im back at square 1, Now instead of the long negative number, I get 1.

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
void GetNumbers( int b [], int size )
{
	int number = 0;
	
	printf( "Please enter your list of ten numbers: " );

	for ( int i = 0 ; i < size; i++ ) {
		scanf( "%d", &number );
		b[ i ] = number;
	}

	printf( "Your array has these values: " );

	for ( int i = 0 ; i < size; i++ ) {
		printf( "%d ", b[ i ] );
	}

	printf( "\n" );
}

int FindLargest ( int array [] )

{
	int maxNumber;
	int temp = array[0] ;

	printf("the largest value is");
	for(int i = 0; i < 10; i++)
	{
		if(array[i] > temp)
	maxNumber = array[i];
	}
	return printf("%d", temp);
	}
OMG, I worked now!
Topic archived. No new replies allowed.