Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the missing code of C functions in the following four files: Please include solution description In Program1.c, write the function int foo(int* a, int

Complete the missing code of C functions in the following four files:

Please include "solution description"

In Program1.c, write the function int foo(int* a, int *b, int c) that is supposed to do the following:

Increment a

Decrement b

Assign (a - b) to c

Return the value of c.

In the main function, declare three integers x, y, z, and assign to them random integer values in the interval [0,10], using rand(). Print the values of x, y, z. Call foo() by appropriately passing x, y, z, as arguments. Print out the values of x, y, z, after foo(), as well as the value returned by foo().

Code:

#include

#include

int foo(int* a, int* b, int c){

/* Increment a */

/* Decrement b */

/* Assign a-b to c */

/* Return c */

}

int main(){

/* Declare three integers x,y and z and initialize them randomly to

values in [0,10] */

/* Print the values of x, y and z */

/* Call foo() appropriately, passing x,y,z as parameters */

/* Print the values of x, y and z */

/* Print the value returned by foo */

/* Is the return value different than the value of z? Why? */

return 0;

}

-------------------------------------------------------------------------------------------

In Program2.c, write the following functions and demonstrate their functionality by calling them (in the given order) from the main function:

struct student* allocate() that allocates memory for 10 students, and returns the pointer.

void generate(struct student* students) that generates random initials and random scores for each of the 10 students, and stores them in the array students. To generate two initial letters of a student you may use the following code: char c1 = rand()%26 + 'A'; char c2 = rand()%26 + 'A'; where % indicates the modulo operation. The random score for each student should take values in the interval [0, 100].

void output(struct student* students) that prints the initials and scores of all students.

void summary(struct student* students) that prints the minimum score, maximum score and average score of the 10 students.

void deallocate(struct student* students) that frees the memory allocated to students. Check that students is not NULL before you attempt to free it.

* Solution description:

*/

Code:

#include

#include

#include

struct student{

char initials[2];

int score;

};

struct student* allocate(){

/*Allocate memory for ten students*/

/*return the pointer*/

}

void generate(struct student* students){

/*Generate random initials and scores for ten students.

The two initial letters must be capital and must be between A and Z.

The scores must be between 0 and 100*/

}

void output(struct student* students){

/*Output information about the ten students in the format:

1. Initials Score

2. Initials Score

...

10. Initials Score*/

}

void summary(struct student* students){

/*Compute and print the minimum, maximum and average scores of the

ten students*/

}

void deallocate(struct student* stud){

/*Deallocate memory from stud*/

}

int main(){

struct student* stud = NULL;

/*call allocate*/

/*call generate*/

/*call output*/

/*call summary*/

/*call deallocate*/

return 0;

}

----------------------------------------------------------------------------------------

In Program3.c, write the function void sort(int* numbers, int n) to sort an array of n integers in the ascending order. In the main function:

Declare an integer n. Allocate memory for an array of n integers using malloc. Fill this array with random numbers using rand(). Make sure that you can handle the case when n=0.

Print the contents of the array.

Pass this array along with n to sort().

Print the contents of the sorted array after sort().

* Solution description:

*/

Code:

#include

void sort(int* number, int n){

/*Sort the array of integeres of length n*/

}

int main(){

/*Declare an integer n and assign it a value of 20.*/

/*Allocate memory for an array of n integers using malloc.*/

/*Fill this array with random numbers, using rand().*/

/*Print the contents of the array.*/

/*Pass this array along with n to the sort() function of part a.*/

/*Print the contents of the array.*/

return 0;

}

---------------------------------------------------------------------------------------------------------

In Program4.c, we will consider the structure student from Program2.c. Modify the function sort() from Program3.c to sort an array of n students based on their initials. The function prototype is void sort(struct student* students, int n). In sort(), if two students have the same first initial, you should compare their second initial. As in Program2.c, initials and scores of students are to be generated randomly by rand(). Make sure that you can handle the case when the user provides input n=0.

Code:

#include

#include

struct student{

char initials[2];

int score;

};

void sort(struct student* students, int n){

/*Sort n students based on their initials*/

}

int main(){

/*Declare an integer n and assign it a value.*/

/*Allocate memory for n students using malloc.*/

/*Generate random IDs and scores for the n students, using rand().*/

/*Print the contents of the array of n students.*/

/*Pass this array along with n to the sort() function*/

/*Print the contents of the array of n students.*/

return 0;

}

--------------------------------------------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

love of humour, often as a device to lighten the occasion;

Answered: 1 week ago

Question

orderliness, patience and seeing a task through;

Answered: 1 week ago

Question

well defined status and roles (class distinctions);

Answered: 1 week ago