Question

In: Computer Science

Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...

Given a program as shown below:

#include <stdio.h>

void function1(void);

void function2 (int, double x);

void main (void)

{

int m;

double y;

m=15;

y=308.24;

printf ("The value of m in main is m=%d\n\n",m);

function1();

function2(m,y);

printf ("The value of m is main still m = %d\n",m);

}

void function1(void)

{

printf("function1 is a void function that does not receive\n\\r values from main.\n\n");

}

void function2(int n, double x)

{

int k,m;

double z;

k=2*n+2;

m=5*n+37;

z=4.0*x-58.4;

printf ("function2 is a void function that does receive\n\\r values from main.The values received from main are:\n\\r\t n=%d \n\r\t x=%lf\n\n", n,x);

printf ("function2 creates three new variable, k, m and z\n\\rThese variable have the values:\n\\r\t 1=%d \n\r\t m=%d \n\r\t z=%lf \n\n",k,m,z);

}

Questions: Please use program given as example in explaination.

  1. Is it necessary that main be the first function defined?
  2. Can a function prototype be written in a function body rather than outside the body of all functions?
  3. Is there any relationship between the variable m in main and the variable m in function2?
  4. What would have happened if we had called function2 from main with two integers or two double in the parameter list instead of an integer and a double?
  5. We used the printf function for this program. Why is there no function prototype for it?

answer all the question please

Solutions

Expert Solution

1.) Is it necessary that main be the first function defined?

  • No, it is not necessary. main() can be defined anywhere in the program. We could put the definitions of function1, and function2 before main() and the program will be the same.

2.) Can a function prototype be written in a function body rather than outside the body of all functions?

  • Function protypes can be written in a funtion but, these function will be in the local scope. That is, we cannot use it outside the function that it is declared in.

3.) Is there any relationship between the variable m in main and the variable m in function2?

  • No, there is no relationship between the variable m in main and the one in function2. Both these variables are only present in the local scope of the function that are declared in and they are visible or accessble outside those functions.

4.) Is there any relationship between the variable m in main and the variable m in function2?

  • function2() expects its arguments to be an int and a double. If we call it with two ints or two doubles, the compiler will try to match this function call with a prototype but it will not find any. So, an error will thrown saying that there is no matching function with the prototype function2(int, int) or function2(double, double)

5.) We used the printf function for this program. Why is there no function prototype for it?

  • The prototype and the defintion of printf() function is already present in the header file stdio.h. We are simply using that function in this program. Redecleration of printf() function is not necessary.

Related Solutions

Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
*Answer in C program* Given a program as shown below: #include <stdio.h> void function1(void); void function2...
*Answer in C program* Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37;...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile,...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile, *outfile; infile = fopen (“c6_3.IN”, “r”); outfile = fopen (“c6_3.OUT”, “w”); k = fscanf (infile, “%lf %lf”, &x[0], &y[0]); fprintf (outfile,”k= %d\n”,k); fprintf (outfile, “value of EOF = %d\n”, EOF); i =1; while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++; num_elem =I; fprintf(outfile,” x[i] y[i] z[i]\n”); For (j=0; j<num_elem; j++) { Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]); fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0; while(cost <= funds) { candies += 1; funds -= cost; cost += 0.1; } printf("%d candies with $%.2f left over.\n",candies,funds); return 0; } When you compile and run this code you get 3 candies with $0.40 left over. Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact,...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void...
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void quicksort(int number[25],int first,int last){ int i, j, pivot, temp; if(first<last) { pivot=first; i=first; j=last; while(i<j) { while(number[i]<=number[pivot]&&i<last) i++; while(number[j]>number[pivot]) j--; if(i<j){ temp=number[i]; number[i]=number[j]; number[j]=temp; } } temp=number[pivot]; number[pivot]=number[j]; number[j]=temp; quicksort(number,first,j-1); quicksort(number,j+1,last); } } int main() { int i, count, number[25]; printf("Enter some elements (Maximum 25): "); scanf("%d",&count); printf("Enter %d elements: ", count); for(i=0;i<count;i++) scanf("%d",&number[i]); quicksort(number,0,count-1); printf("The Sorted Order is: "); for(i=0;i<count;i++) printf(" %d",number[i]); return...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000];...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000]; for(i=0;i<1000;i++) a[i]=1;    for(i=2;i<1000;i++) { if(a[i]==1){ for(j=i+1;j<1000;j++) {if(j%(i)==0) a[j]=0; } } } printf("print numbers are:\n"); for(i=2;i<=1000;i++) if(a[i]==1) printf("%d, ",i); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT