147k views
0 votes
1. Create and invoke a function that returns a value. Show an example of how this is done.

2. Create and invoke a function that does not return a value. This called a void function. Show an example of a void function and how it is called from the main program.
3. What is passing by value vs by reference. Show examples.
4. What is a function prototype. Show an example of a function prototype
5. Define what we mean by a variable’s scope and lifetime. Show an example to help you define this concept.
6. What is a one dimensional array. Show an example of declaring a one dimensional array.
7. Give an example of how you get data into a one dimensional array from the keyboard.
8. Give an example of how to display data from #7 above.
9. Give an example of how to pass a one-dimensional array to a function
10. Define parallel one-dimensional arrays and show an example that demonstrate the use of parallel arrays.
11. What is a two dimensional array. Show an example of declaring a two dimensional array.
12. Give an example of how you get data into a two dimensional array from the keyboard.
13. Give an example of how to display data from #12 above.

User Jared S
by
4.2k points

1 Answer

7 votes

Answer:

Answers are given below with appropriate comments for explanation

Step-by-step explanation:

1)

int min(int first, int second) //function which returns min value of given two values.

{

int answer;

if (first < second)

result = first;

else

result = second;

return result;

}

2)

#include<stdio.h>

void min(int first, int second) //function which returns min value of given two values.

{

int answer;

if (first < second)

result = first;

else

result = second;

printf("Result is = %d",result);

}

void main(){

int a1=5;

int a2=3;

min(a1,a2);//calling void function

}

3)

pass by value...just passing values..it won't reflect actual parameters

#include<stdio.h>

void min(int first, int second) //function which returns min value of given two values.

{

int answer;

if (first < second)

result = first;

else

result = second;

printf("Result is = %d",result);

}

void main(){

int a1=5;

int a2=3;

min(a1,a2);//calling void function

}

-----------------------------

pass by reference

-----------------------------

#include<stdio.h>

void min(int *first, int *second) //function which returns min value of given two values.

{

int answer;

if (first < second)

result = first;

else

result = second;

printf("Result is = %d",result);

}

void main(){

int a1=5;

int a2=3;

min(&a1,&a2);//calling void function

}

-----------------------------------------------------------------------------------------------------------------

4)

function prototype means declaring function declaration without body at top of program...actual function after main declaration.

#include<stdio.h>

void min(int first, int second); //function prototype declaration

void main(){

int a1=5;

int a2=3;

min(a1,a2);//calling void function

}

//actual function declaration

void min(int first, int second) //function which returns min value of given two values.

{

int answer;

if (first < second)

result = first;

else

result = second;

printf("Result is = %d",result);

}

---------------------------------------------------------------------------------

5)

int main(){

int global=5; //global variable

int n=1;

for(i=1;i<=n;i++){

int global =3; //local variable

printf("Local variable scope = %d",global); //it prints 3

}

printf("Global variable scope = %d",global); //it prints 5

}

---------------------------------------------------------------------------

6)

int main(){

int a[5] = {1,2,3,4,5}; //one dimensional array declaration

}

------------------------------------------

7)

int main(){

int array[5];

int i;

printf("Enter array values");

for(i=0;i<5;i++){

scanf("%d",&a[i]); //reading array values from user

}

}

8)

int main(){

int array[5];

int i;

printf("Enter array values");

for(i=0;i<5;i++){

scanf("%d",&array[i]); //reading array values from user

}

printf("Printing data of array entered");

for(i=0;i<5;i++){

printf("%d",array[i]); //Printing array values from user

}

----------------------------------------

9)

#include<stdio.h>

void printArray(int a[]){//array as parameter

int i;

printf("Printing data of array entered");

for(i=0;i<5;i++){ //printing data here

printf("%d",a[i]); //Printing array values from user

}

}

int main(){

int array[5];

int i;

printf("Enter array values");

for(i=0;i<5;i++){

scanf("%d",&array[i]); //reading array values from user

}

printArray(array); //passing array as parameter

}

--------------------------------------------------------------

User Hrant Nurijanyan
by
4.3k points