CPS5401 Assignment 101222
CPS5401 Assignment 101222
This assignment has to be completed independently. You may consult the Internet and/or the instruc-
tor/TA and your classmates but you must write this assignment yourself.
Deadline: 11:59 pm, October 15 2022.
The TA will discuss the solutions during the class on Monday, October 17 2022.
2. On your local machine, go inside the cloned directory, and create a sub-directory called class-101222.
Put all required work for this assignment in this sub-directory.
Commands to add, commit and push:
$ git add class-101222
Call this program maximum-10lastname.c and commit it to your repository under the designate sub-
directory class-101222.
Your output should look like:
Hint: You can set max=array[0] and then check if each entry of the array is larger than the max.
2. (60 pts) The C program below performs the following tasks:
(a) It introduces another library called stdlib.h.
(b) It generates an array of a user-defined length n.
(c) This program generates a random number by calling rand(), scales it and inputs this number in the
array.
The function rand() returns a random number of type int but we want to store entries of type
double. Hence, we enable this involving the function (double). This enables us to cast the variable
as a double instead.
(d) Next, this C program stores the entries of the array in a txt file called array-data.txt.
(e) It then seeks to calculate the largest entry of this array.
1 # include < stdio .h >
2 # include < stdlib .h > // needed for rand ()
3 int main () {
4
5 int loop , n ;
6 printf ( " enter the number of r a n d o m l y g e n e r a t e d numbers : " ) ;
7 scanf ( " % d " ,& n ) ;
8
9 double x [ n ];
10
11 for ( int i =0; i < n ; ++ i )
12 {
13 // rand () is a random number generator of type int .
14 // This can be a large size int hence by multiplying
15 // with 1e -6 , we ensure that the number is not too large .
16 x [ i ] = ( double ) ( rand () ) *1 e -6;
17 printf ( " x [% d ] = % lf \ n " ,i , x [ i ]) ;
18 }
19
20 // this creates a file array - data . txt to write into if the file exists ,
21 // it will merely open the file to write into it .
22 char * filename = " array - data . txt " ;
23
24 // open the file for writing
25 FILE * fp = fopen ( filename , " w " ) ;
26 if ( fp == NULL )
27 {
28 printf ( " Error opening the file % s " , filename ) ;
29 return -1; //
30 }
31 // write to the text file
32 for ( int i = 0; i < n ; i ++)
33 {
34 fprintf ( fp , " % lf \ n " ,x [ i ]) ;
35 // fprintf ( fp , " This is the line #% d \ n " , i + 1) ;
36 // fprintf ( fp , " This is the entry x [% d ]= % lf \ n " , i , x [ i ]) ;
37 }
38 // close the file
39 fclose ( fp ) ;
40
41
42 double largest ;
43 largest = x [0];
44
45 for ( loop = 1; loop < n ; loop ++)
46 {
47 if ( largest < x [ loop ] )
48 {
49
50 } // if condition
51 printf ( " largest for loop = % d is : % lf \ n " , loop , largest ) ;
52 }// for - loop
53
54 printf ( " Largest element of array is % lf \ n " , largest ) ;
55
56 }
3. (20 points) In the C program below, we open the file array-data.txt generated in question 2 with the
intention of reading entries from the file, storing each entry in a variable called number and printing the
number to the screen.
Your task is modify the code provided below and write a C program called ques3-lastname.c. This C
program should: