For this assignment, you must write pseudocode and C code for several sub-functi
ID: 673685 • Letter: F
Question
For this assignment, you must write pseudocode and C code for several sub-functions that use pointers and arrays, and a main() function that calls your sub-functions, printing the specified values.
The sub-functions you write will have the following prototypes and perform the operations described:
int sum(int array[], int asize); /* returns the sum of the elements of a one dimensional array.*/
int swap( int *pA, int *pB ); /* returns a 1 if the integer stored at *pA is greater then the integer stored at *pB and swaps the two integers. returns 0 otherwise.*/
int sort_1pass(int array[], int asize ; /* makes one pass through the array, swapping pairs of values that are not in ascending order, using the swap() function above. Returns a 1 if any values were swapped and returns 0 otherwise.*/
void sort(int array[], asize); /* sorts the values in array in ascending order using sort_1pass() */
void printarray(int array[], asize); /* prints the contents of the array on one line*/
Use a preprocessor #define directive before main() to set the array size to 9 elements.
The main() function should perform the following actions, and call the sub-functions above where appropriate:
Declare and initialize the int array[] so that each array element contains one digit of your Red ID number, so if your Red ID was 876543210, the value of the first few elements in the array would be array[0]=8, array[1]=7, array[2]=6, and so on. The array declaration and initialization should look like this:
int array[] = {8,7,6,...};
Next main() should print all 9 array values in the array on a single line prior to processing.
Print the sum of the the digits in the array using the sum() function above.
Call function sort(), which then calls sort_1pass(), which calls swap() to swap individual pairs.
Print the 9 values in the array on one line after each sort_1pass() through the array.
The last line printed should be the 9 digits of your Red ID, sorted in ascending order.
Print the median (middle) digit in the sorted array.
Print the average of the digits in the array, accurate to 2 decimal places.
Explanation / Answer
#define SIZE 9
int sum(int array[], int asize)
{
int total=0;
for(int i=0;i<asize;i++)
total=total+array[i];
return total;
}
int swap( int *pA, int *pB )
{
int *temp;
if((int)*pA > (int)*pB)
{
*pA=temp;
temp=*pB;
*pB=*pa;
return 1;
}
else return 0;
}
int sort_1pass(int array[], int asize)
{
bool swapFlag=false;
for(int i=0;i<asize;i++)
{
if(array[i]>array[i+1])
{
swap(&array[i],&array[j]);
swapFlag=true;
}
if(swapFlag)
return 1;
else return 0;
}
void sort(int array[], asize)
{
for(int j=0;j<asize;j++)
sort_1pass(array,asize);
}
void printarray(int array[], asize)
{
for(int j=0;j<asize;j++)
printf("%d",array[j]);
}
int main()
{
int array[]={8,7,6,5,4,3,2,1,0};
printarray(array,SIZE);
int total=sum(array,SIZE);
printf("Total sum is %d",total);
sort(array,SIZE);
printarray(array,SIZE);
int medianPosition=floor(SIZE/2);
printf("Median is %d",array[medianPosition]);
double average=total/SIZE;
printf("Average is %0.2f",average);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.