By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

Converting WHILE LOOP into FUNCTIONS

P: 26
Hello everyone!:) i need your help again..i got there a code of a while loop program and our prof let us convert it into functions. can anyone help me please. It must be converted into 3 types.

1. void only
2. void but with parameter
3. w/out void but with return value

Here's the code:

1 #include <stdio.h>
2 #include <iostream.h>
3 #include <conio.h>
4
5 int main(void)
6
7 {
8
9 int i, j;
10 i=0;
11 while (i<=4) {
12 j=0;
13 while (j<=4) {
14 j++;
15 }
16 i++;
17 }
18 i++;
19 printf("%d", i*j);
20
21 return 0;
22 }

I run this program already and the output is 30. I really appreciate your help everyone. Thank you very much
Jan 24 '07 #1
Share this Question
Share on Google+
12 Replies


100+
P: 1,646
Hello everyone!:) i need your help again..i got there a code of a while loop program and our prof let us convert it into functions. can anyone help me please. It must be converted into 3 types.

1. void only
2. void but with parameter
3. w/out void but with return value

Here's the code:

1 #include <stdio.h>
2 #include <iostream.h>
3 #include <conio.h>
4
5 int main(void)
6
7 {
8
9 int i, j;
10 i=0;
11 while (i<=4) {
12 j=0;
13 while (j<=4) {
14 j++;
15 }
16 i++;
17 }
18 i++;
19 printf("%d", i*j);
20
21 return 0;
22 }

I run this program already and the output is 30. I really appreciate your help everyone. Thank you very much
An example of one of these would be to create a function that returns the product of i and j

Expand|Select|Wrap|Line Numbers
  1. int product(int i, int j) {
  2.    return i * j;
  3. }
and then use this in your code:
Expand|Select|Wrap|Line Numbers
  1. printf("%d", product(i, j));
Good luck
Jan 24 '07 #2

P: 26
Thanks for that help madam :) but how about for the void only and void with parameter? got any ideas?
Jan 24 '07 #3

100+
P: 1,646
Thanks for that help madam :) but how about for the void only and void with parameter? got any ideas?
A void only function takes no parameters and returns nothing
Expand|Select|Wrap|Line Numbers
  1. void say_hi(void) {
  2.    printf("%s", "Hello World");
  3. }
and is used by
Expand|Select|Wrap|Line Numbers
  1. say_hi();
void with parameters:
Expand|Select|Wrap|Line Numbers
  1. void say_something(char *message) {
  2.    printf("%s", message);
  3. }
and is used:
Expand|Select|Wrap|Line Numbers
  1. say_something("Hello World");
  2.  
Jan 24 '07 #4

P: 26
maam villa, i tried to create a void with parameter code but it says syntax error and our prof told me that it's not correct. my code is below..

#include <stdio.h>

void prn_value(int x, int y);

int main(void)

{
int i, j;
i=0;
while (i<=4) {
j=0;
while (j<=4) {
j++;
}
i++;
}
i++;
prn_value (i,j);
return 0;
}

void prn_value(int x, int y)

{

int value=i*j;
printf("The product is %d", value);

}

what's wrong with my code boss?
Jan 26 '07 #5

P: 26
i created another code...

include <iostream.h>
#include <conio.h>

int product (int i, int j);

int main(void)

{

product (i, j);


}

int product (int i, int j)

{


i=0;
while (i<=4) {
j=0;
while (j<=4) {
j++;
}
i++;
}
i++;
cout << i*j;

return i * j;
}

it says declaration syntax error on int main(void).

can somebody help me..

thanks a lot..
Jan 26 '07 #6

Expert 100+
P: 1,510
the formal parameters to
Expand|Select|Wrap|Line Numbers
  1. void prn_value(int x, int y)
  2. {
  3.     int value=i*j;
  4.     printf("The product is %d", value);
  5. }
  6.  
are x and y but you use i and j in the code

be consistent either use i &j or x and y throughout the function e.g.
Expand|Select|Wrap|Line Numbers
  1. void prn_value(int x, int y)
  2. {
  3.     int value=x*y;  // ** replace i and j with x and y
  4.     printf("The product is %d", value);
  5. }
  6.  
Jan 26 '07 #7

P: 51
hi:you must defne all you variables in the begining of mail function.and then send whatever you want to Product function.
include <iostream.h>
#include <conio.h>

int product (int i, int j);

int main(void)

{
/*you must define some variables here*/
product (i, j);


}

int product (int i, int j)

{


i=0;
while (i<=4) {
j=0;
while (j<=4) {
j++;
}
i++;
}
i++;
cout << i*j;

return i * j;
}
Jan 26 '07 #8

P: 51
on the other hand you can write a Only Void function with defining two Global parameter in the begining of program
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int i;
  3. int j;
  4. void product(void);
  5. int main()
  6.   {....
  7.    product(); 
  8.    ....}
  9.  
Jan 26 '07 #9

P: 26
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. int product (int x, int y);
  5.  
  6. int main(void)
  7.  
  8. {
  9.     clrscr();
  10.     int a, b, total;
  11.     total=product(a,b);
  12.     printf("The output is %d", total);
  13.     getch();
  14.     return 0;
  15.  
  16.  
  17. }
  18.  
  19. int product (int x, int y)
  20.  
  21. {
  22.  
  23.     x=0;
  24.     while (x<=4) {
  25.        y=0;
  26.        while (y<=4) {
  27.           y++;
  28.        }
  29.        x++;
  30.     }
  31.     x++;
  32.  
  33.     return x * y;
  34.  
  35.  
  36. }
  37.  
so this is the right one, right???
Jan 26 '07 #10

Expert 100+
P: 1,510
not sure what your program is suposed to do, e.g. you pass in parameters a and b but without any values, the values are set up inside the function - why pass in parameters? you don't need them.

e.g. you could define the variables inside the function so
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2.  
  3. int product (void);
  4.  
  5. int main(void)
  6.  
  7. {
  8.     int total;
  9.     total=product();
  10.     printf("The output is %d", total);
  11.     getchar();
  12.     return 0;
  13. }
  14.  
  15. int product (void)
  16.  
  17. {
  18.     int x, y;
  19.     x=0;
  20.     while (x<=4) {
  21.        y=0;
  22.        while (y<=4) {
  23.           y++;
  24.        }
  25.        x++;
  26.     }
  27.     x++;
  28.     return x * y;
  29. }
  30.  
note that it is not recommended to use header file <conio.h>, clrscr() and getch(). They are not standard C/C++ and are not available with many compilers.

It is also not recommended to use
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2.  
which is a deprecated header. Modern compilers use
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
however, if you have an old compiler (such as Turbo C V3.0) it will not support this.
Jan 26 '07 #11

P: 26
And here's one for the Only Void code...

#include <iostream.h>
#include <stdlib.h>

int i, j;

void product (void);

int main(void)
{
system("CLS");
product();
}

void product (void)

{
i=0;
while (i<=4) {
j=0;
while (j<=4) {
j++;
}
i++;
}
i++;
cout << i*j << endl;
}

are they all correct boss?
Jan 26 '07 #12

P: 26
#include <iostream.h>

int product (void);

int main(void)

{
int total;
total=product();
printf("The output is %d", total);
getchar();
return 0;
}

int product (void)

{
int x, y;
x=0;
while (x<=4) {
y=0;
while (y<=4) {
y++;
}
x++;
}
x++;
return x * y;
}


ic.. so what's the difference of the code similar to the one i posted before? it also gave me the same output even if i have

int a, b, total;
total=product (a,b)

inside the main body and (int x, int y) parameter upon declaring the function.

can you explain it to me sir..thank you.
Jan 26 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.