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

1. Create and invoke a function that returns a value. Show an example of how thi

ID: 664658 • Letter: 1

Question

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 demonstarate the use of parallel arrays.

11. What is a twodimensional 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.

Explanation / Answer

Here are examples...and comments added


1)

int min(int first, int second) //functin 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) //functin 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) //functin 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 referrence
-----------------------------
#include<stdio.h>
void min(int *first, int *second) //functin 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 protype 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 protype declaration
void main(){
int a1=5;
int a2=3;
min(a1,a2);//calling void function
}
//actual function declaration
void min(int first, int second) //functin 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 variale scope = %d",global); //it prints 3
}
printf("Global variale 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 arrya 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 arrya values from user
}
printf("Printing data oof 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 oof 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 arrya values from user
}
printArray(array); //passing array as parameter
}

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

10)
#include<stdio.h>
void printArray(int a[], int b[]){// parallel array as parameter
int i;
printf("Printing data oof first array entered");
for(i=0;i<5;i++){ //printing data here
printf("%d",a[i]); //Printing array values from user
}
printf("Printing data oof second array entered");
for(i=0;i<5;i++){ //printing data here
printf("%d",b[i]); //Printing array values from user
}
}
int main(){
int array[5],b[5];
int i;
printf("Enter array values one for first array and second one for second array");
for(i=0;i<5;i++){
scanf("%d",&aarray[i]); //reading parallel arrays values from user
scanf("%d",&b[i]);
}
printArray(a,b); //passing array as parameter
}

------------------------------------------------------
11)
two dimensional array is two 1d arrays per each row

#include<stdio.h>
void main(){
int twoDimensionalArray[5][5];//declaration...can store upto 25 elements
}

---------------------------------------------------------
12)
#include<stdio.h>
void main(){
int twoDimensionalArray[5][5];//declaration...can store upto 25 elements
printf("Reading values from user for two dimensional");
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
scanf("%d",&twoDimensionalArray[i][j]); //readind values
}
}
}

-------------------------------------------------
13)
#include<stdio.h>
void main(){
int twoDimensionalArray[5][5];//declaration...can store upto 25 elements
printf("Reading values from user for two dimensional");
printf("Enter values");
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
scanf("%d",&twoDimensionalArray[i][j]); //readind values
}
}
printf("Printing array values");
for(i=0;i<5;i++){
for(j=0;j<4;j++){
pritnf("%d",twoDimensionalArray[i][j]); //printing two dimensional array values
}
}
}