Documente Academic
Documente Profesional
Documente Cultură
Note : There are two variables called minimumValue, one in main and another
within the function
computeMinimum. Although they have the same name, they are different variables
within different scopes. One is local to main and another is local to the function
computeMinimum.
Recall : scope of variables covered in previous sessions.
Question : Why do we need to pass the size of the array to the function
computeMinimum, is it not enough to pass the array alone?
Answer : No explicit array bounds checking in C. Size is not encoded when the
array is passed. Recall that the array name is a pointer, i.e. when an array is
passed, only the start address is passed.
Question : Is it possible to return both the minimum value and index?
Answer : A function can return at most one thing. Returning multiple
entities can be indirectly accomplished using pointers. If it is necessary to return
both, one way is to create a new struct which contains both these entities and
return a pointer to that struct. structs will be covered in later classes.
Example 2 : Implement selection sort using functions
/*Sorting Elements of an array in ascending order using insertion sort algorithm and
a function*/
#define MAX_SIZE 100
#include<stdio.h>
void selectionSort(int data[],int numElements); //Function declaration
int main()
{
int data[MAX_SIZE],numElements,i;
printf("Enter number of terms(should be less than 100): ");
scanf("%d",&numElements);
printf("Enter elements: ");
for(i=0;i<numElements;i++)
{
scanf("%d",&data[i]);
}
selectionSort(data,numElements); //Function call
printf("In ascending order: ");
for(i=0; i<numElements; i++)
printf("%d\t",data[i]);
return 0;
}
//Function implementing selection sort.
void selectionSort(int arr[],int numElements)
{
int i,j,temp;
for(i=0;i<numElements;i++)
{
for(j=i+1;j<numElements;j++)
{
if(arr[i]>arr[j]) //swap values of arr[i] and arr[j]
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
Question : Will the function sort only a copy of the array (like in the swap
program), what needs to be done to ensure the original array created in main is
sorted?
Answer :
Arrays are in fact pointers. Thus when an array is passed to a function
(the array named data created in main), the pointer corresponding to the array
location (specifically the address of data[0]) is passed. Since the mechanism was
pass by pointers and not pass by value, the values corresponding memory
locations in the original array itself are changed.
Exercise :
Implement insertion sort and bubble sort. First
modularize the problem into separate functions by identifying
arguments and return types. Now implement any one sorting
technique.
findAndPrintQuadrants(x,y,numPoints);
printf("++++++++++++++++++++++\n");
printf("Computing via function and printing in main\n");
printf("++++++++++++++++++++++\n");
//Get quadrants of the points via a function
getQuadrants(x,y,quadrants,numPoints);
for(i=0;i < numPoints;i++)
{
printf("%d : (%f,%f) ",i,x[i],y[i]);
switch(quadrants[i])
{
case 1 :
printf("First quadrant\n");
break;
case 2 :
printf("Second quadrant\n");
break;
case 3 :
printf("Third quadrant\n");
break;
case 4 :
printf("Fourth quadrant\n");
break;
default :
printf("Unknown quadrant\n");
}
}
return 0;
}
//A function which takes as inputs two arrays which contain the x and y
coordinates and
//prints the quadrant to which the point belongs to.
void findAndPrintQuadrants(float x[],float y[],int N) //N = number of points
{
int i=0;
if(N <= 0)
{
printf("Number of points must be a positive integer\n");
return;
}
for(i = 0;i < N;i++)
{
if(x[i] >= 0 && y[i] >= 0)
printf("%d : (%f,%f) First Quadrant\n",i,x[i],y[i]);
else if(x[i] < 0 && y[i] >= 0)
printf("%d : (%f,%f) Second Quadrant\n",i,x[i],y[i]);
else if(x[i] < 0 && y[i] < 0)
printf("%d : (%f,%f) Third Quadrant\n",i,x[i],y[i]);
else //if(x[i] >= 0 && y[i] < 0)
printf("%d : (%f,%f) Fourth quadrant\n",i,x[i],y[i]);
}
}
//A function which takes as inputs two arrays which contain the x and y
coordinates and
//computes the quadrants to which the points belong to. The result is stored in
another array
//which is also passed as input to the function.
void getQuadrants(float x[],float y[],int quadrants[],int N)
{
int i=0;
if(N <= 0)
{
printf("Number of points must be a positive integer\n");
return;
}
for(i = 0;i < N;i++)
{
if(x[i] >= 0 && y[i] >= 0)
quadrants[i] = 1;
else if(x[i] < 0 && y[i] >= 0)
quadrants[i] = 2;
else if(x[i] < 0 && y[i] < 0)
quadrants[i] = 3;
else //if(x[i] >= 0 && y[i] < 0)
quadrants[i] = 4;
}
}
Exercise : Modularize the following program into multiple functions. Consider a list
of prices of books represented as one dimensional array. Traverse the list and
modify the prices according to the following conditions:
(i) if price > 250, offer discount of 10%. Update the price with respect to
discount.
(ii) if price > 500, offer discount of 25%. Update the price w.r.t discount.
Modularize it into following functions.
(i) readList(): Populate prices of the items.
(ii) printList(): Prints the price list.
(iii) modifyList(): Modify the prices in the list according to the conditions given
above.
Identify input arguments and return types for all the functions. Also write main
function where the functions are called.