d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Beginner C Question > Please Help!
Add Reply New Topic New Poll
Member
Posts: 1,938
Joined: Dec 27 2007
Gold: 4.01
May 18 2014 01:41pm
My professor gave us code which is supposed to accept an array of floating points numbers and doubles every number in the array (as in, multiply the numbers in the array by two). Here's what he gave us:

function definition
Code
void doubler (int integers[], int length, int a)
{
int i = 0;
for(i = 0; i < length; i++)
{
integers[i] = integers[i]*2;
}
}


function call
Code
void doubler (int integers[], int length, int a);

#include <stdio.h>
int main(void)
{
int a[4] = {0, 1, 2, 3};
doubler(a,4);
return 0;
}


however, when i run the code (using ChIDE), it returns the following:

ERROR: number of argument is 2, need 3 arguments
ERROR: syntax error before or at line 16 in file 'hw8_q6.c'
==>: doubler(a,4);
BUG: doubler(a,4);<== ???
ERROR: cannot execute command

I've looked through our notes and the textbook but can't figure out how to get the code to work.

Paying fg if anyone can help!


This post was edited by BlueAnGeL07 on May 18 2014 01:59pm
Member
Posts: 4,316
Joined: Nov 6 2013
Gold: 83,875.00
May 18 2014 01:57pm
ur fucnction has 3 parameters and u call it with 2
what is the point of last param (a) ?
Also u should be able to make it with just one param (the array)
u can calculate the length of it in the function

sizeof (x) / sizeof (x[0])

This post was edited by john197 on May 18 2014 02:01pm
Member
Posts: 24,802
Joined: Mar 7 2010
Gold: 22,202.22
May 18 2014 01:58pm
Quote (john197 @ May 18 2014 02:57pm)
ur fucnction has 3 parameters and u call it with 2
what is the point of last param (a) ?


this, even if its a null call, you need to add a third parameter when you call the function as its defined as having three parameters
Member
Posts: 50,343
Joined: Apr 3 2008
Gold: 0.00
May 18 2014 05:02pm
http://forums.d2jsp.org/topic.php?t=70760193&f=122&p=475144111

Quote

ur fucnction has 3 parameters and u call it with 2
what is the point of last param (a) ?
Also u should be able to make it with just one param (the array)
u can calculate the length of it in the function

sizeof (x) / sizeof (x[0])


This is wrong. When you pass an array to any function you pass its pointer which is always either 4 or 8 bytes long. So infact when you tried to use his method of finding the array size inside a funciton it would actually return the size of the pointer address and not the actual data.

Here is an example of why it is wrong:

Code

#include <stdio.h>
#include <stdlib.h>

void test(int array[]);

int main() {
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
printf("Size of array outside of funciton is: %d\n", sizeof(a) / sizeof(a[0]));
test(a);
return 0;
}

void test(int array[]) {
printf("Size of array inside of function is: %d\n", sizeof(array) / sizeof(array[0]));
}


Size of array outside of funciton is: 9
Size of array inside of function is: 1

Process returned 0 (0x0) execution time : 0.034 s
Press any key to continue.


When passing an array in C you must always pass the length of it as well unless it is an array of fixed length, even then you should pass it anyways.

@OP: Your call to the funciton is failing because you have a useless variable (int a) inside the funciton prototype at the top. Remove it as it is not being used and your code will compile.

From abduct
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll