# CS 1050: Homework Assignment 3

CS 1050

Homework Assignment 3

Directions

Complete the following homework assignment using the description given in each section.

Purpose

? Use command line arguments to read input from the user.

? Use rand() and srand() functions from the library to generate random numbers.

? Use malloc function to allocate memory and create arrays.

? Use pointers arithmetic and notation in implementation.

Submission information

Submit this assignment on the blackboard using the link provided under the section assignments

(submit HW3). SUBMIT ONLY the .C file. All the HW assignment must be submitted by the due

date on the blackboard.

Description

The homework assignment is on students grades in a class. There are n students in the class. The

number n is read from the command prompt (similar to prelab-7 and lab-7). Each student have a

student id which is a random number between 900-999. There are three courses and students have

scores for each of the three courses. The score in a course is a random number between 50-100.

Here assume that the lowest score is going to be 50. For the assignment you have to create a

student id array (int) and three (int) arrays for students scores in the three courses. A random

number between 50-100 can be created by first dividing the number by 51 and then adding 50 to

it similarly random number 900-999 can be generated. Using the rand() function initialize the

student id array and course score arrays with random numbers (same as previous labs). Once this

is set up you have to use this information to perform various operations like compute the grade of

each student, median score for each course, search student grade etc.

Command Line arguments

As explained above the total number of student in class is read from the command prompt

(similar to prelab-7 (and lab 7)) . User is going the provide the input from the command line in

the following

./a.out 10

Here 10 is the size of the class and will be stored in the argv array as string so you need to convert

this string number "10" to integer number 10. To convert string to integer make use of function

atoi (as shown in the example below). The variable size contains an integer number which can be

used making the function calls. For more detail on command line refer to prelab 7.

int main(int argc , char **argv)

{

if(argc<2)

{

printf("\n Not enough parameters \n");

return 0;

}

int size =atoi(argv[1]);

.

.

.

}

Implement following functions for the homework assignment.

int* create_id(int)- This function takes an integer number (size) and use it to create an integer

array using malloc function. Once the array is created store a random number between 900-999 in

the integer array. While generating the student id make sure that none of the student id repeated

so if the size is 10 then there should be 10 unique id (between 900-999) stored in the integer

array. Your function should correctly implement this logic. At the end return the integer pointer.

int* create_score(int)- This function similar to create_id function takes an integer number

(size) and use it to create an integer array using malloc function. Once the array is created store a

random number between 50-100 in the integer array. At the end return the integer pointer.

float* average_score(int*, int*, int*, int)- This function takes the scores in three courses

and the size and calculates the average score for each student. The average score is stored in a

float array which is created by using a malloc function. At the end it return the pointer to a float

array.

char *calculate_grade(float*, int)- This function takes the average score of students in three

courses and the size and calculates the students grade using the criteria below. It stores the student

grade in a character array which is again created by using a malloc function. At the end it returns

the pointer to character array.

Use following grading criterion to calculate the student’s grade in the course.

average score =90 A

between 80-89 B

between 70-79 C

between 60-69 D

below 60 F

float median(int*, int)- This function takes an integer pointer (scores in a course) and size and

calculate the median score in the course. At the end it returns the median value(float).

Median: Median is the value located in the middle for a given set of numbers. To compute the

median first sort the numbers and then select the middle value as a median. The numbers in a

given set can be even or odd median value can be calculated for both such cases as shown in the

example below.

For odd size input

x={2,4,1,0,9}

sorted x={0,1,2,4,9}

median value is 2

For even size input

x= {2, 5, 0, 9}

sorted x={0, 2 ,5, 9}

median =(2+5)/2

=3.5

void print_scorecard( int*, int*, int*, int*, float*, char*, int)- This function takes student

id array , student score arrays, average score array , letter grade array and print the information as

shown in the sample output below.

int search_id(int *, int, int)- This functions takes the student id array, its size and an id that need

to be searched in the student id array if the id is present then it returns the index of the id

otherwise it returns -1.

void histogram(char *, int)- This functions takes grade array and size and display the

histogram of grades as shown in the sample output below.

main(): In the main first perform an error check using argc variable to make sure that user has

entered all the inputs from the command prompt if not display an error message and quit the

program (see the prelab 7 document for more detail). As shown above use atoi function to convert

a string to an integer number. To use this function include stdlib.h file in the code. Call create_id

and create_score functions using this size to create student id, course score arrays. Call

average_score and calculate_grade functions to calculate the average score and students grade.

Call function print_scorecard to display the result as shown in the sample output below.

Note:

1. Similar to prelab-7 and lab-7 use malloc function to allocate space to the pointer.

2. Like the prelab-7 and lab-7 use POINTER NOTATION AND POINTER ARITHMETIC

only to implement the homework assignment.

Sample Output

[[email protected] HW3]$ ./a.out

Not enough arguments

[[email protected] HW3]$ ./a.out 10

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

ID Course1 Course2 Course3 Average Grade

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

973 53 59 90 67.33 D

914 66 51 57 58.00 F

923 85 78 74 79.00 C

911 77 57 100 78.00 C

952 54 98 85 79.00 C

932 85 67 66 72.67 C

935 51 53 67 57.00 F

984 78 57 99 78.00 C

916 90 88 72 83.33 B

908 79 91 70 80.00 B

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

Enter the student id to search:908

The student with id 908 has the following scores

Course1:79

Course2:91

Course3:70

Average score:80.00

Grade B

Median score for course1 is 77.50

Median score for course2 is 63.00

Median score for course3 is 73.00

Grade Histogram

A:

B:**

C:*****

D:*

E:

F:**

[[email protected] HW3]$ ./a.out 9

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

ID Course1 Course2 Course3 Average Grade

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

906 82 86 94 87.33 B

992 57 69 71 65.67 D

939 50 84 54 62.67 D

965 88 52 89 76.33 C

997 86 80 68 78.00 C

943 98 76 94 89.33 B

923 59 98 54 70.33 C

946 51 77 82 70.00 C

948 60 80 74 71.33 C

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

Enter the student id to search:933

The student id 933 is not present.

Median score for course1 is 60.00

Median score for course2 is 80.00

Median score for course3 is 74.00

Grade Histogram

A:

B:**

C:*****

D:**

E:

F:

Guidelines for Grading HW-3

40 Points Possible

General

If your program does not compile, or produce any input/output (I/O) because

most of the source code is commented out then your HW will receive a grade of

NO POINTS. For any partial credit your C program must not only compile but also

produce some valid I/O that meets HW specifications. Use pointers and pointer

notation in implementation. Use of array notation will be given zero.

-5 points – Not having proper format.

5 points – Reading size correctly from the command prompt and performing an

error check using command line arguments

5 points – Initializing the three arrays correctly with random numbers.

5 points – Calculating the average score correctly.

5 points – Calculating the grade correctly.

5 points – Implementing search_id function correctly.

7 points – Printing information correctly using print_scorecard function.

8 points – Printing the grade histogram correctly.

Homework Assignment 3

Directions

Complete the following homework assignment using the description given in each section.

Purpose

? Use command line arguments to read input from the user.

? Use rand() and srand() functions from the library to generate random numbers.

? Use malloc function to allocate memory and create arrays.

? Use pointers arithmetic and notation in implementation.

Submission information

Submit this assignment on the blackboard using the link provided under the section assignments

(submit HW3). SUBMIT ONLY the .C file. All the HW assignment must be submitted by the due

date on the blackboard.

Description

The homework assignment is on students grades in a class. There are n students in the class. The

number n is read from the command prompt (similar to prelab-7 and lab-7). Each student have a

student id which is a random number between 900-999. There are three courses and students have

scores for each of the three courses. The score in a course is a random number between 50-100.

Here assume that the lowest score is going to be 50. For the assignment you have to create a

student id array (int) and three (int) arrays for students scores in the three courses. A random

number between 50-100 can be created by first dividing the number by 51 and then adding 50 to

it similarly random number 900-999 can be generated. Using the rand() function initialize the

student id array and course score arrays with random numbers (same as previous labs). Once this

is set up you have to use this information to perform various operations like compute the grade of

each student, median score for each course, search student grade etc.

Command Line arguments

As explained above the total number of student in class is read from the command prompt

(similar to prelab-7 (and lab 7)) . User is going the provide the input from the command line in

the following

./a.out 10

Here 10 is the size of the class and will be stored in the argv array as string so you need to convert

this string number "10" to integer number 10. To convert string to integer make use of function

atoi (as shown in the example below). The variable size contains an integer number which can be

used making the function calls. For more detail on command line refer to prelab 7.

int main(int argc , char **argv)

{

if(argc<2)

{

printf("\n Not enough parameters \n");

return 0;

}

int size =atoi(argv[1]);

.

.

.

}

Implement following functions for the homework assignment.

int* create_id(int)- This function takes an integer number (size) and use it to create an integer

array using malloc function. Once the array is created store a random number between 900-999 in

the integer array. While generating the student id make sure that none of the student id repeated

so if the size is 10 then there should be 10 unique id (between 900-999) stored in the integer

array. Your function should correctly implement this logic. At the end return the integer pointer.

int* create_score(int)- This function similar to create_id function takes an integer number

(size) and use it to create an integer array using malloc function. Once the array is created store a

random number between 50-100 in the integer array. At the end return the integer pointer.

float* average_score(int*, int*, int*, int)- This function takes the scores in three courses

and the size and calculates the average score for each student. The average score is stored in a

float array which is created by using a malloc function. At the end it return the pointer to a float

array.

char *calculate_grade(float*, int)- This function takes the average score of students in three

courses and the size and calculates the students grade using the criteria below. It stores the student

grade in a character array which is again created by using a malloc function. At the end it returns

the pointer to character array.

Use following grading criterion to calculate the student’s grade in the course.

average score =90 A

between 80-89 B

between 70-79 C

between 60-69 D

below 60 F

float median(int*, int)- This function takes an integer pointer (scores in a course) and size and

calculate the median score in the course. At the end it returns the median value(float).

Median: Median is the value located in the middle for a given set of numbers. To compute the

median first sort the numbers and then select the middle value as a median. The numbers in a

given set can be even or odd median value can be calculated for both such cases as shown in the

example below.

For odd size input

x={2,4,1,0,9}

sorted x={0,1,2,4,9}

median value is 2

For even size input

x= {2, 5, 0, 9}

sorted x={0, 2 ,5, 9}

median =(2+5)/2

=3.5

void print_scorecard( int*, int*, int*, int*, float*, char*, int)- This function takes student

id array , student score arrays, average score array , letter grade array and print the information as

shown in the sample output below.

int search_id(int *, int, int)- This functions takes the student id array, its size and an id that need

to be searched in the student id array if the id is present then it returns the index of the id

otherwise it returns -1.

void histogram(char *, int)- This functions takes grade array and size and display the

histogram of grades as shown in the sample output below.

main(): In the main first perform an error check using argc variable to make sure that user has

entered all the inputs from the command prompt if not display an error message and quit the

program (see the prelab 7 document for more detail). As shown above use atoi function to convert

a string to an integer number. To use this function include stdlib.h file in the code. Call create_id

and create_score functions using this size to create student id, course score arrays. Call

average_score and calculate_grade functions to calculate the average score and students grade.

Call function print_scorecard to display the result as shown in the sample output below.

Note:

1. Similar to prelab-7 and lab-7 use malloc function to allocate space to the pointer.

2. Like the prelab-7 and lab-7 use POINTER NOTATION AND POINTER ARITHMETIC

only to implement the homework assignment.

Sample Output

[[email protected] HW3]$ ./a.out

Not enough arguments

[[email protected] HW3]$ ./a.out 10

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

ID Course1 Course2 Course3 Average Grade

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

973 53 59 90 67.33 D

914 66 51 57 58.00 F

923 85 78 74 79.00 C

911 77 57 100 78.00 C

952 54 98 85 79.00 C

932 85 67 66 72.67 C

935 51 53 67 57.00 F

984 78 57 99 78.00 C

916 90 88 72 83.33 B

908 79 91 70 80.00 B

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

Enter the student id to search:908

The student with id 908 has the following scores

Course1:79

Course2:91

Course3:70

Average score:80.00

Grade B

Median score for course1 is 77.50

Median score for course2 is 63.00

Median score for course3 is 73.00

Grade Histogram

A:

B:**

C:*****

D:*

E:

F:**

[[email protected] HW3]$ ./a.out 9

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

ID Course1 Course2 Course3 Average Grade

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

906 82 86 94 87.33 B

992 57 69 71 65.67 D

939 50 84 54 62.67 D

965 88 52 89 76.33 C

997 86 80 68 78.00 C

943 98 76 94 89.33 B

923 59 98 54 70.33 C

946 51 77 82 70.00 C

948 60 80 74 71.33 C

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

Enter the student id to search:933

The student id 933 is not present.

Median score for course1 is 60.00

Median score for course2 is 80.00

Median score for course3 is 74.00

Grade Histogram

A:

B:**

C:*****

D:**

E:

F:

Guidelines for Grading HW-3

40 Points Possible

General

If your program does not compile, or produce any input/output (I/O) because

most of the source code is commented out then your HW will receive a grade of

NO POINTS. For any partial credit your C program must not only compile but also

produce some valid I/O that meets HW specifications. Use pointers and pointer

notation in implementation. Use of array notation will be given zero.

-5 points – Not having proper format.

5 points – Reading size correctly from the command prompt and performing an

error check using command line arguments

5 points – Initializing the three arrays correctly with random numbers.

5 points – Calculating the average score correctly.

5 points – Calculating the grade correctly.

5 points – Implementing search_id function correctly.

7 points – Printing information correctly using print_scorecard function.

8 points – Printing the grade histogram correctly.

You'll get a 300.5KB .ZIP file.