0% found this document useful (0 votes)
85 views

CPS5401 Assignment 101222

This document provides instructions for a class activity assignment involving C programming with arrays and reading/writing data to/from a text file. It outlines 3 questions: 1. Write a C program to find the maximum of an array of 10 numbers and commit it to a Bitbucket repository. 2. Complete code to generate a random number array, store it in a file, find the largest entry, and explain how omitting a library would affect the code. 3. Modify provided code to open a file, read entries into an array, and print the array.

Uploaded by

Mike Myers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

CPS5401 Assignment 101222

This document provides instructions for a class activity assignment involving C programming with arrays and reading/writing data to/from a text file. It outlines 3 questions: 1. Write a C program to find the maximum of an array of 10 numbers and commit it to a Bitbucket repository. 2. Complete code to generate a random number array, store it in a file, find the largest entry, and explain how omitting a library would affect the code. 3. Modify provided code to open a file, read entries into an array, and print the array.

Uploaded by

Mike Myers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CPS 5401: Class Activity

C Program: Arrays and writing/reading data


Natasha S. Sharma. PhD 2022-10-12
to/from a txt file
100 points

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.

Bitbucket Repository Instructions


You will need to use your Bitbucket repository: “CPS5401Codes-username”, where username is something that
helps the TA and the instructor to distinguish you from the others, such as your UTEP account name.
If you haven’t already done this in the Bitbucket repository settings, grant the instructor (search for nsshar-
[email protected]) and the TA (search for [email protected]) the write privilege.
1. Make a clone of the Bitbucket repository on your local machine.

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

$ git commit -m "relevant message"


$ git push origin
You can try as many times to modify the submission before the deadline.
The TA and the instructor will only retrieve the work from Bitbucket repositories; no other form of submission
(including via email) is accepted.
Make sure that you have your Bitbucket account setup properly ahead of time.
No extension will be made in any circumstances, even if the reason is a last-minute power outage or network
disruption.

1. (20 pts) Write a C program to find the maximum of an array of 10 numbers.

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, −9, 0}

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 }

Your task is to:


I. complete any missing lines of code to complete the aforementioned tasks.
II. To match the line number of the code to the tasks (a) to (e).
For example, task (a) matches to line 2.
III. What happens to your code if you omit including the library stdlib.h?
Turn in the answer to part I. in the form of a C code called questn-2I-lastname.c and upload it in the
designated location.
Turn in the answer to parts II. and III. in a txt file called questn-2rest-lastname.txt and upload it in
the designated location.
Please attempt question 3 only after you have attempted question 2 and write this code in the
same sub-directory as the code from question 2.

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:

• open the file array-data.txt generated in question 2


• read entries from this txt file
• store each entry in an array called my array and printing the array entries to the screen.
Upload this code into the designated sub-directory.

1 # include < stdio .h >


2 # include < stdlib .h >
3
4 int main ()
5 {
6 double number ;
7 FILE * in_file = fopen ( " array - data . txt " , " r " ) ; // only open a pre - exisiting file in←-
read mode .
8
9 if (! in_file ) // equivalent to saying if ( in_file == NULL )
10 {
11 printf ( " oops , file can 't be read \ n " ) ;
12 exit ( -1) ;
13 }
14 // attempt to read the next line and store
15 // the value in the " number " variable
16 while ( fscanf ( in_file , " % lf " , & number ) == 1 )
17 {
18 printf ( " We just read % lf \ n " , number ) ;
19 }
20 }

You might also like