A program is already given to you. There are five problems in this skeleton version of the program, each is 10 points.
All you got to do is complete the missing code in each function. What the function does is clearly stated in the name of the function.
// ASSIGNMENT ON FUNCTIONS
#include <stdio.h>
// Problem 1: Compile with gcc func_assignment.c -Wall
// There are some warnings because there is a mismatch
between
// data type passed and defined.
// Find out, fix anyway you prefer and remove the
warngings
void swapWithRef ( unsigned char *x, unsigned char *y )
{
// PROBLEM 2: Complete this function code
// THIS SWAPS TWO VALUES IN VARIABLES PASSED BY REFERENCE
}
// PROBLEM 3
// complete the missing code to compute sum of all array
elements
// passed in array data
unsigned int ComputeSum ( unsigned int data[ ] , unsigned int len
)
{
unsigned int sum = 0 ;
// returns the sum
return sum;
}
// PROBLEM 4
// Input *ptr
// Input len
// Reference pointer variable *sum
//
void ComputeSumOfOddNumbers ( unsigned int *ptr, unsigned int len,
unsigned int *sum )
{
// complete the missing code . THis function should
compute the sum of all
// odd cell values numbers in the array
}
// Problem 5
void Increment(unsigned int *ptr , unsigned int len )
{
// complete the missing code.
//This function will increment all cell values by one
}
int main ( )
{
char x = 'B', y = 'a' ;
printf ( " x = %c y = %c \n", x, y ) ;
swapWithRef ( &x, &y ) ;
printf ( " x = %c y = %c \n", x, y );
unsigned int data [ ] = { 11, 18, 7, 6 , 9};
// Ideally you should compute len. but it's okay for
now.
unsigned int len = 5 ;
printf ( "Sum of all elements %d \n", ComputeSum (
data, len ) ) ;
unsigned int oddSum = 0 ;
ComputeSumOfOddNumbers ( data, len , &oddSum
);
printf ( "Sum of all odd numbers %d \n", oddSum )
;
oddSum = 0 ;
unsigned int *ptr = data ;
Increment ( ptr, len );
int i;
for ( i = 0 ; i < len; i++ )
printf ( "i= %d value = %d \n", i,
data[i] );
}
#include <stdio.h>
void swapWithRef ( unsigned char *x, unsigned char *y )
{
// PROBLEM 2: Complete this function code
// THIS SWAPS TWO VALUES IN VARIABLES PASSED BY REFERENCE
unsigned char p = *x;
*x = *y;
*y = p;
}
// PROBLEM 3
// complete the missing code to compute sum of all array elements
// passed in array data
unsigned int ComputeSum ( unsigned int data[ ] , unsigned int len )
{
unsigned int sum = 0 ;
for(int i=0 ; i<len; ++i){
sum = sum + data[i];
}
// returns the sum
return sum;
}
// PROBLEM 4
// Input *ptr
// Input len
// Reference pointer variable *sum
//
void ComputeSumOfOddNumbers ( unsigned int *ptr, unsigned int len, unsigned int *sum )
{
// complete the missing code . THis function should compute the sum of all
// odd cell values numbers in the array
*sum = 0;
for(int i=0 ; i<len; ++i){
if(ptr[i] %2 == 1)
*sum = *sum + ptr[i];
}
}
// Problem 5
void Increment(unsigned int *ptr , unsigned int len )
{
// complete the missing code.
//This function will increment all cell values by one
for(int i=0 ; i<len; ++i){
ptr[i]= ptr[i] + 1;
}
}
int main ( )
{
unsigned char x = 'B', y = 'a' ;
printf ( " x = %c y = %c \n", x, y ) ;
swapWithRef ( &x, &y ) ;
printf ( " x = %c y = %c \n", x, y );
unsigned int data [ ] = { 11, 18, 7, 6 , 9};
// Ideally you should compute len. but it's okay for now.
unsigned int len = 5 ;
printf ( "Sum of all elements %d \n", ComputeSum ( data, len ) ) ;
unsigned int oddSum = 0 ;
ComputeSumOfOddNumbers ( data, len , &oddSum );
printf ( "Sum of all odd numbers %d \n", oddSum ) ;
oddSum = 0 ;
unsigned int *ptr = data ;
Increment ( ptr, len );
int i;
for ( i = 0 ; i < len; i++ )
printf ( "i= %d value = %d \n", i, data[i] );
}
===============================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
Get Answers For Free
Most questions answered within 1 hours.