Hi, I need little help here ^_^

//I am learning about how to use subroutine but I have problem in the result
//I expect the result will be:
b[0] = 1
b[1] = 2
b[2] = 9
b[3] = 16
b[4] = 25
//but, the result is
b[0] = -2
b[1] = 1989087586
b[2] = 1989434300
b[3] = 4200272
b[4] = 2293544

//Can someone help me? I will appreciate that ^_^
The code shown below....
-------------------------------------------------------
#include <stdio.h>

int fungsi(int a[5]){
int i;
int b[5];
for(i=0;i<=4;i++)
{
if (a[i]>=3)
{b[i] = a[i]*a[i];}
else {b[i] = a[i];}
}

return b[5];
}



int main ()
{
int b[5];
int c[5] = {1,2,3,4,5};
int fungsi(int c[5]);

int i;
for(i=0;i<=4;i++)
{printf("\nb[%i] = %i",i,b[i]);}
return 0;}
-------------------------------------------------------
closed account (D80DSL3A)
You have a scope issue - the array b[5] in main() is different from the array b[5] in fungsi().
Also, the function isn't called right and the array is not being passed correctly. You will need to pass both arrays to fungsi().
Try:
1
2
3
4
5
6
7
8
9
10
11
void fungsi(int a[], int b[]){
int i;
for(i=0;i<=4;i++)
{
if (a[i]>=3)
{b[i] = a[i]*a[i];}
else {b[i] = a[i];}
}

return;// why return a value?
}

Then call it like this fungsi(c,b); in main().
That should fix it.
Hi, thank you for the quick reply ^_^ . Umm, I still having same problem the result still
b[0] = -2
b[1] = 1989087586
b[2] = 1989434300
b[3] = 4200272
b[4] = 2293544
the code is:
----------------------------------------------------
#include <stdio.h>

int fungsi(int a[], int b[]){
int i;
for(i=0;i<=4;i++)
{
if (a[i]>=3)
{b[i] = a[i]*a[i];}
else {b[i] = a[i];}
}

return ;}



int main ()
{
int b[5];
int c[5] = {1,2,3,4,5};
int fungsi (c,b);

int i;
for(i=0;i<=4;i++)
{printf("\nb[%i] = %i",i,b[i]);}
return 0;}
-----------------------------------------------
can U help me ^_^?
@jansen
The fix for your program is very simple. Remove the int in front of int fungsi(c,b); Then, it'll call the function correctly..
Hey whitenite, thanks a lot it works!!!!!!!!!!!!! ^_^ Nice!!! Thank you very much!
@jansen
You're very welcome.
Hmm, and I want to ask again
in subroutine, for example I wrote

int function (x,y){
...
int d;
return d;
}

int main(){
int c = function(a,b)
printf("%i",c);
return 0;}

It will mean I replace x ,y with a,b right?
and the output is int d
and then int c= int d


And I still confuse when i use return 0, return a,b,c,...etc or even when i not use it,

Thank You!!! ^_^
@jansen
It will mean I replace x ,y with a,b right? Yes.
the output is int d Yes, IF in the function you have d to equal something. The way it stands now, d doesn't really equal anything. It's not even initialized. If, for example you have in the function..
1
2
int d = x*y;
return d;
then your int c in main, will print the value of the returned d.
Hey I have problem here ^_^
If I have
int a = 10;
int b = 2;
int c = (a&b) << 1

the result of c is 4 but I don't know the process
what does (a&b) means?
what does <<1 means?
--------------------------------------------
int a = 5;
int b = 9;
int c = 7;
int d;
d= a>b? a>c? a : c: b>c? b:c
//d=9 it is the max number of a,b,c
//What does "d= a>b? a>c? a : c: b>c? b:c" means? I still confused
--------------------------------------------
int a = 11;
float b,c,d;
b = a/2;
c = a/2.;
d = a/b;

printf("b = %f\n",b);
printf("c = %f\n",c);
printf("d = %f\n",d);
//the result is
//b = 5.000000
//c = 5.500000
//d = 2.200000
//I still confused how to divide int by float....
-----------------------------------------------------
int i =5,x;
switch(i)
{
default:
x=100;
case 1:
x=10;break;

case 2:
x=20;break;

case 3:
x=30;break;

case 4:
x=40;break;
}
printf("%i", x);
//the result is 10, why not 100?
-------------------------------------------------
Can Someone Help me? I will appreciate that! ^^
Hey I have problem here ^_^
If I have
int a = 10;
int b = 2;
int c = (a&b) << 1

the result of c is 4 but I don't know the process
what does (a&b) means?
what does <<1 means?


a=10 equal 1 0 1 0
b=2 equal 0 0 1 0
c=a&b equal 0 0 1 0=2

c=c<<1 mean c=c*1^2


int a = 5;
int b = 9;
int c = 7;
int d;
d= a>b? a>c? a : c: b>c? b:c
//d=9 it is the max number of a,b,c
//What does "d= a>b? a>c? a : c: b>c? b:c" means? I still confused


i will give you an example

int a=10;
int b=8;
int c =(a>b) ? a : b;///==>c=10 ( max of a and b)



int a = 11;
float b,c,d;
b = a/2;
c = a/2.;
d = a/b;

printf("b = %f\n",b);
printf("c = %f\n",c);
printf("d = %f\n",d);
//the result is
//b = 5.000000
//c = 5.500000
//d = 2.200000
//I still confused how to divide int by float....


give you an example

int a=10;
int b=3;
float d=a/b;//d=3
float c= (float)a/b;// c=3.3333333


i
nt i =5,x;
switch(i)
{
default:
x=100; break;
case 1:
x=10;break;

case 2:
x=20;break;

case 3:
x=30;break;

case 4:
x=40;break;
}
printf("%i", x);
//the result is 10, why not 100?
--------------------------------------------


you lost break
Topic archived. No new replies allowed.