void updateRange(int *x, int *y[5]);
int main (void)
{
int accidentCount;
int range[5];
printf("Enter an accident count (negative to end): \n", -1);
scanf("%d", accidentCount);
updateRange(&accidentCount, &range[5])
while( accidentCount != -1 )
{
printf("Enter an accident count (negative to end): ");
}
system ("pause");
return (0);
}
void updateRange(int *x, int *y[5])
{
if(*x >= 500){
y[5]+= 1;}
else if(500 > *x && *x >= 400){
y[4]+= 1;}
else if(400 > *x && *x >= 300){
y[3] += 1;}
else if(300 > *x && *x >= 200){
y[2] += 1;}
else if(200 > *x && *x >= 100){
y[1] += 1;}
else if(100 > *x && *x >= 0){
y[0] += 1;}
}
20 Replies - 533 Views - Last Post: 28 June 2011 - 09:52 PM
#1
Passing arrays to functions
Posted 28 June 2011 - 04:09 PM
This is the start of my program. For some reason it won't pass the array back. Can someone help me?
Replies To: Passing arrays to functions
#2
Re: Passing arrays to functions
Posted 28 June 2011 - 04:15 PM
What include files are you using?
Does this compile without errors or warnings?
If not post the complete error messages exactly as they appear in your development environment.
Look closely at the following line:
Do you notice anything missing?
Jim
Does this compile without errors or warnings?
If not post the complete error messages exactly as they appear in your development environment.
Look closely at the following line:
updateRange(&accidentCount, &range[5])
Do you notice anything missing?
Jim
#3
Re: Passing arrays to functions
Posted 28 June 2011 - 04:20 PM
I am including the stdio.h. I did see I missed ;. But now the problem i have is that the loop won't stop.
#4
Re: Passing arrays to functions
Posted 28 June 2011 - 04:30 PM
Actually, that looks like too much information:
is trying to pass an array of pointers, but you've got an array of ints.
Try:
with
updateRange(&accidentCount, &range[5]);
is trying to pass an array of pointers, but you've got an array of ints.
Try:
updateRange(accidentCount, range[5]);
with
void updateRange(int x, int y[5])
{
#5
Re: Passing arrays to functions
Posted 28 June 2011 - 04:33 PM
I think I got it to pass but now I can't get it to stop. When I type -1 it doesn't stop.
#6
Re: Passing arrays to functions
Posted 28 June 2011 - 04:53 PM
Okay guys thanks getting there now. So here is what I am looking to do. I need to have the input of the number which is accountCount, pass that to updateRange, then display range needs to take the counts from updateRange and print them. I can't figure out if I am on the right track. If you can look at it and see if I am really far off. Thanks.
include <stdio.h>
void updateRange(int x, int y[5]);
void displayRange(int a[5], int b[5]);
int main (void)
{
int accidentCount;
int range[5];
printf("Enter an accident count (negative to end): ", -1);
scanf("%d", accidentCount);
updateRange(accidentCount, range[5]);
while( accidentCount != -1 )
{
printf("Enter an accident count (negative to end): ");
scanf("%d", accidentCount);
updateRange(accidentCount, range[5]);
}
displayRange(range[],
system ("pause");
return (0);
}
void updateRange(int x, int y[5])
{
if(x >= 500){
y[5]+= 1;}
else if(500 > x && x >= 400){
y[4]+= 1;}
else if(400 > x && x >= 300){
y[3] += 1;}
else if(300 > x && x >= 200){
y[2] += 1;}
else if(200 > x && x >= 100){
y[1] += 1;}
else if(100 > x && x >= 0){
y[0] += 1;
}
void displayRange( int a[5], b[5])
{
for (i =0; i < 5; ++i)
printf("Range\t\tFrequency");
printf(i"-"i + 99"\t\t%d\n", Range[i]);
}
#7
Re: Passing arrays to functions
Posted 28 June 2011 - 04:54 PM
First you need to change your compiler settings to enable warnings and set the warnings to maximum. These are the warnings I received when I compiled your code:
These warnings should considered errors.
Line 14:
You are passing 2 parameters but you do not tell printf where to print the variable and that variables type.
Line: 15
You need to pass a pointer to scanf() so this should be:
Jim
Quote
main.c||In function ‘main’:|
main.c|14|warning: too many arguments for format|
main.c|15|warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’|
main.c|17|warning: passing argument 2 of ‘updateRange’ from incompatible pointer type|
main.c|3|note: expected ‘int **’ but argument is of type ‘int *’|
||=== Build finished: 0 errors, 3 warnings ===|
main.c|14|warning: too many arguments for format|
main.c|15|warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’|
main.c|17|warning: passing argument 2 of ‘updateRange’ from incompatible pointer type|
main.c|3|note: expected ‘int **’ but argument is of type ‘int *’|
||=== Build finished: 0 errors, 3 warnings ===|
These warnings should considered errors.
Line 14:
printf("Enter an accident count (negative to end): \n", -1);
You are passing 2 parameters but you do not tell printf where to print the variable and that variables type.
Line: 15
scanf("%d", accidentCount);
You need to pass a pointer to scanf() so this should be:
scanf("%d", &accidentCount);
Notice the ampersand.Jim
#8
Re: Passing arrays to functions
Posted 28 June 2011 - 05:33 PM
Okay I put the ampersands on there but I am still erroring out alot.
#include <stdio.h>
void updateRange(int x, int y[5]);
void displayRange(int a[5], int b[5]);
int main (void)
{
int accidentCount;
int range[5];
printf("Enter an accident count (negative to end): ", -1);
scanf("%d", &accidentCount);
updateRange(&accidentCount, &range[5]);
while( accidentCount != -1 )
{
printf("Enter an accident count (negative to end): ");
scanf("%d", &accidentCount);
updateRange(&accidentCount, &range[5]);
}
displayRange(range[]);
system ("pause");
return (0);
}
void updateRange(int x, int y[5])
{
if(x >= 500){
y[5]+= 1;}
else if(500 > x && x >= 400){
y[4]+= 1;}
else if(400 > x && x >= 300){
y[3] += 1;}
else if(300 > x && x >= 200){
y[2] += 1;}
else if(200 > x && x >= 100){
y[1] += 1;}
else if(100 > x && x >= 0){
y[0] += 1;
}
void displayRange( int a[5], b[5])
{
for (i =0; i < 5; ++i)
printf("Range\t\tFrequency");
printf(i"-"i + 99"\t\t%d\n", Range[i]);
}
#9
Re: Passing arrays to functions
Posted 28 June 2011 - 05:46 PM
Quote
Okay I put the ampersands on there but I am still erroring out alot.
Post the errors exactly as they appear in your development environment.
Jim
#10
Re: Passing arrays to functions
Posted 28 June 2011 - 05:59 PM
Executing gcc.exe...
gcc.exe "C:\My Documents\school\C++ Class Files\program7.c" -o "C:\My Documents\school\C++ Class Files\program7.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\My Documents\school\C++ Class Files\program7.c: In function `main':
C:\My Documents\school\C++ Class Files\program7.c:15: warning: passing arg 1 of `updateRange' makes integer from pointer without a cast
C:\My Documents\school\C++ Class Files\program7.c:22: warning: passing arg 1 of `updateRange' makes integer from pointer without a cast
C:\My Documents\school\C++ Class Files\program7.c:25: error: syntax error before ']' token
C:\My Documents\school\C++ Class Files\program7.c: In function `updateRange':
C:\My Documents\school\C++ Class Files\program7.c:49: error: syntax error before "b"
C:\My Documents\school\C++ Class Files\program7.c: In function `displayRange':
C:\My Documents\school\C++ Class Files\program7.c:52: error: `i' undeclared (first use in this function)
C:\My Documents\school\C++ Class Files\program7.c:52: error: (Each undeclared identifier is reported only once
C:\My Documents\school\C++ Class Files\program7.c:52: error: for each function it appears in.)
C:\My Documents\school\C++ Class Files\program7.c:54: error: syntax error before string constant
Execution terminated
gcc.exe "C:\My Documents\school\C++ Class Files\program7.c" -o "C:\My Documents\school\C++ Class Files\program7.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\My Documents\school\C++ Class Files\program7.c: In function `main':
C:\My Documents\school\C++ Class Files\program7.c:15: warning: passing arg 1 of `updateRange' makes integer from pointer without a cast
C:\My Documents\school\C++ Class Files\program7.c:22: warning: passing arg 1 of `updateRange' makes integer from pointer without a cast
C:\My Documents\school\C++ Class Files\program7.c:25: error: syntax error before ']' token
C:\My Documents\school\C++ Class Files\program7.c: In function `updateRange':
C:\My Documents\school\C++ Class Files\program7.c:49: error: syntax error before "b"
C:\My Documents\school\C++ Class Files\program7.c: In function `displayRange':
C:\My Documents\school\C++ Class Files\program7.c:52: error: `i' undeclared (first use in this function)
C:\My Documents\school\C++ Class Files\program7.c:52: error: (Each undeclared identifier is reported only once
C:\My Documents\school\C++ Class Files\program7.c:52: error: for each function it appears in.)
C:\My Documents\school\C++ Class Files\program7.c:54: error: syntax error before string constant
Execution terminated
#11
Re: Passing arrays to functions
Posted 28 June 2011 - 06:11 PM
Quote
C:\My Documents\school\C++ Class Files\program7.c:15: warning: passing arg 1 of `updateRange' makes integer from pointer without a cast
And line 15 is:
updateRange(&accidentCount, &range[5]);
And the function signature is:
void updateRange(int x, int y[5])
So for parameter 1 your are passing a pointer to accidentCount but your function is expecting an integer.
Your function prototype, function implementation and function call must all agree on the number and type of parameters.
I think you should review how to pass parameters into your functions. Here is a link to 2 such pages: Functions I and Functions II. Then you may want to review how arrays work.
Also notice how I determined the line where your compiler detected the error.
Jim
This post has been edited by jimblumberg: 28 June 2011 - 06:12 PM
#12
Re: Passing arrays to functions
Posted 28 June 2011 - 06:25 PM
I guess I will go back to my book. The things that are on those links don't show practical examples that is the real problem.
#13
Re: Passing arrays to functions
Posted 28 June 2011 - 06:51 PM
Okay this works but why can't I add another function.
Thanks for the help
#include <stdio.h>
void updateRange( int *x, int *y[5]);
int
main(void)
{
int accidentCount;
int range[5];
printf("Enter an accident count (negative to end): ", -1);
scanf("%d", &accidentCount);
updateRange(&accidentCount, &range[5]);
while ( accidentCount != -1 ){
printf("Enter an accident count (negative to end): ", -1);
scanf("%d", &accidentCount);
updateRange(&accidentCount, &range[5]);
}
}
void updateRange(int *x, int *y[5])
{
if (*x >= 500){
*y[5]+= 1;}
else if (500 > *x && *x >= 400){
*y[4]+= 1;}
else if(400 > *x && *x >= 300){
*y[3] += 1;}
else if(300 > *x && *x >= 200){
*y[2] += 1;}
else if(200 > *x && *x >= 100){
*y[1] += 1;}
else if(100 > *x && *x >= 0){
*y[0] += 1; }
}
Thanks for the help
#14
Re: Passing arrays to functions
Posted 28 June 2011 - 06:52 PM
Please read the tutorials on functions in my signature.
This:
is very, very wrong for one thing. Arrays are indexed from 0 to length of array - 1. You have a 5-element array, and you're accessing 6 array elements, which means you're lucky it's not just crashing.
This:
updateRange(&accidentCount, &range[5]);
is very, very wrong for one thing. Arrays are indexed from 0 to length of array - 1. You have a 5-element array, and you're accessing 6 array elements, which means you're lucky it's not just crashing.
#15
Re: Passing arrays to functions
Posted 28 June 2011 - 06:55 PM
One thing you should also do is compile more often. Make one or two small changes then re-compile your code. Always make sure that warnings are enabled and when you compile that the program compiles without any errors or warnings before making any other changes. This way you will at least know where the errors are and can concentrate on the few lines you added or changed. When I start a new program I start with:
I compile this code first to insure my development environment is still operating as I expect.
Then I will start slowly adding code, compiling often, until I have the solution to my problem. I do not start the actual coding until I have an understanding of the problem and an idea of how to proceed.
Also since you are using C you might want to look at this tutorial on C functions. Also you may want to start at the beginning of this tutorial.
Jim
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
I compile this code first to insure my development environment is still operating as I expect.
Then I will start slowly adding code, compiling often, until I have the solution to my problem. I do not start the actual coding until I have an understanding of the problem and an idea of how to proceed.
Also since you are using C you might want to look at this tutorial on C functions. Also you may want to start at the beginning of this tutorial.
Jim
This post has been edited by jimblumberg: 28 June 2011 - 07:01 PM
|
|

New Topic/Question
Reply



MultiQuote





|