Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could someone please tell me how functions work, and why my defined fuction for

ID: 672447 • Letter: C

Question

Could someone please tell me how functions work, and why my defined fuction for this wont work. I'm really new to c, and im just a little lost.

int floyds_triangle(int input);

int main ()
{
   int input;
   printf("Enter the number of rows of Floyd's triangle to print: ");
   scanf("%d ", &input);
   //error management thing  
       if (input <= -1)
   {
       printf("The number of rows cannot be negative for Floyd's triangle. ");
   }

   return 0;
}

int floyds_triangle (int input)
{
   int inc, add, finish = 1;
   for(inc = 1; inc <= input; ++inc)
   {

       for(add = 1; add <= inc; ++add)
       {
           printf("%d ", finish);
           ++finish;
       }
       printf(" ");
   }
   return floyds_triangle;
}

Explanation / Answer

A Function is a small program(piece of code) which does a task(like calculating/printing etc) and returns back.

Function will be called from the main() program, by writing the name of the Function with parameters. When the main program control reaches and executes the function name, program control will go to the definition of the function and executes the statements in the body of the function and on completion, the control returns back to the original place/program where the function is called from.

Also Fuction has to be declared and defined. Your code has both.

The main missing things in the program is:

Function floyds_triangle() is not called anywhere in the main() program. Hence the function was not executed and did not print the triangle as per valid input.

I have modified the program and added the following line, appropriately in the else condition.

floyds_triangle(input);

All other modifications to the program code are supplied with appropriate comments in the code.

Please find the modified working program:


void floyds_triangle(int input);//void is written instead of int because you are not returning
//any value from the function, but just printing, so void is appropriate
int main ()
{
int input;
printf("Enter the number of rows of Floyd's triangle to print: ");
scanf("%d ", &input);
//error management thing
if (input <= -1) //suggestion:use (input <= 0), because valid input can start from 1 and above
{
printf("The number of rows cannot be negative for Floyd's triangle. ");
}
else{
floyds_triangle(input);//Calling the Function
}
return 0;
}
void floyds_triangle (int input)//void is written instead of int because you are not returning
//any value from the function, but just printing, so void is appropriate
{
int inc, add, finish = 1;
for(inc = 1; inc <= input; ++inc)
{
for(add = 1; add <= inc; ++add)
{
printf("%d ", finish);
++finish;
}
printf(" ");
}
//return floyds_triangle;
//above line is commented because --you can return a value
//of type int, float etc., and not the name of the function
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote