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.
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a pointer to char str[] */ void array_to_ptr () { int n=0, len; char str[ ] = "Hello World!"; len = strlen(str); for( n=0; n<len; n++) {     putc(str[n], stdout); } printf("\nlength = %d\n", n); } int contains (char * str, char c); int * makearray(int n); int main (void) { printf("Question #2 - array_to_ptr:\n"); array_to_ptr();   printf("\n------------------------------------\n\n"); printf("Question #3 - contains:\n"); printf("Test #1: "); if...
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...
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,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...