Converting WHILE LOOP into FUNCTIONS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Synapse
    New Member
    • Dec 2006
    • 26

    Converting WHILE LOOP into FUNCTIONS

    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
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by Synapse
    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

    Code:
    int product(int i, int j) {
       return i * j;
    }
    and then use this in your code:
    Code:
    printf("%d", product(i, j));
    Good luck

    Comment

    • Synapse
      New Member
      • Dec 2006
      • 26

      #3
      Thanks for that help madam :) but how about for the void only and void with parameter? got any ideas?

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by Synapse
        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
        Code:
        void say_hi(void) {
           printf("%s", "Hello World");
        }
        and is used by
        Code:
        say_hi();
        void with parameters:
        Code:
        void say_something(char *message) {
           printf("%s", message);
        }
        and is used:
        Code:
        say_something("Hello World");

        Comment

        • Synapse
          New Member
          • Dec 2006
          • 26

          #5
          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?

          Comment

          • Synapse
            New Member
            • Dec 2006
            • 26

            #6
            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..

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              the formal parameters to
              Code:
              void prn_value(int x, int y)
              {
              	int value=i*j;
              	printf("The product is %d", value);
              }
              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.
              Code:
              void prn_value(int x, int y)
              {
              	int value=x*y;  // ** replace i and j with x and y
              	printf("The product is %d", value);
              }

              Comment

              • khajeddin
                New Member
                • Nov 2006
                • 51

                #8
                hi:you must defne all you variables in the begining of mail function.and then send whatever you want to Product function.
                Originally posted by Synapse
                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;
                }

                Comment

                • khajeddin
                  New Member
                  • Nov 2006
                  • 51

                  #9
                  on the other hand you can write a Only Void function with defining two Global parameter in the begining of program
                  Code:
                  #include<stdio.h>
                  int i;
                  int j;
                  void product(void);
                  int main()
                    {....
                     product(); 
                     ....}

                  Comment

                  • Synapse
                    New Member
                    • Dec 2006
                    • 26

                    #10
                    Code:
                    #include <iostream.h>
                    #include <conio.h>
                    
                    int product (int x, int y);
                    
                    int main(void)
                    
                    {
                    	clrscr();
                    	int a, b, total;
                    	total=product(a,b);
                    	printf("The output is %d", total);
                    	getch();
                    	return 0;
                    
                    
                    }
                    
                    int product (int x, int y)
                    
                    {
                    
                    	x=0;
                    	while (x<=4) {
                    	   y=0;
                    	   while (y<=4) {
                    	      y++;
                    	   }
                    	   x++;
                    	}
                    	x++;
                    
                    	return x * y;
                    
                    
                    }
                    so this is the right one, right???
                    Last edited by horace1; Jan 26 '07, 02:52 PM. Reason: added code tags

                    Comment

                    • horace1
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 1510

                      #11
                      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
                      Code:
                      #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;
                      }
                      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
                      Code:
                      #include <iostream.h>
                      which is a deprecated header. Modern compilers use
                      Code:
                      #include <iostream>
                      using namespace std;
                      however, if you have an old compiler (such as Turbo C V3.0) it will not support this.

                      Comment

                      • Synapse
                        New Member
                        • Dec 2006
                        • 26

                        #12
                        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?

                        Comment

                        • Synapse
                          New Member
                          • Dec 2006
                          • 26

                          #13
                          #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.

                          Comment

                          Working...