Question

A program is already given to you.  There are five problems in this skeleton version of the...

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] );
}

Homework Answers

Answer #1

#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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i...
Given the following code: int x = 0; int y = 10; int *ptr = &x;...
Given the following code: int x = 0; int y = 10; int *ptr = &x; *ptr = -55; x += -52; ptr = &y; *ptr += 52; printf("%d\n", x); What is printed to the screen?
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
Fill in the code for the function below called containsAvgValue. It takes as input a float...
Fill in the code for the function below called containsAvgValue. It takes as input a float array of any length and returns 1 if the array happens to contain a value that equals the average of all the values in the array or 0 otherwise. For example, when give the array [2.0, 2.0] it should return 1 since the average value is 2.0 and the array contains that value. To help you, I’ve included a function that computes the average...
Answer the questions based on the program given below. a) Write the function prototype of fnFunny...
Answer the questions based on the program given below. a) Write the function prototype of fnFunny function. b) Write the output produced by the above program #include <stdio.h> int main (void){       unsigned short i =4;       unsigned long j = 6 ;       short k = 2 ;       k = fnFunny ( i , j );       printf("i = %hu, j = %lu, k = %hi\n", i, j, k);       return 0; } short fnFunny (unsigned short i,...
The following program implements a key-value data structure in which values are stored based on a...
The following program implements a key-value data structure in which values are stored based on a string keyword. Currently, the program allows new keys and values to be entered, but there is no function to retrieve the values given a search key. Complete the get_value function to retrieve a value given a search key. The function should print "INVALID KEY" if the key is not present. Do not remove any code OR add any code to main. You should only...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT