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]);...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#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 <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        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 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT