Structures Files
-
Upload
krithika-rajendran -
Category
Documents
-
view
215 -
download
0
Transcript of Structures Files
-
8/2/2019 Structures Files
1/6
CSE180 Computer Programming in C Lab
Structures & Files
1. Create a structure to specify data of customers in a bank. The data to be stored
is: Account no, Name and balance in the account.
a) Write a function to print the account number and name of each customer
whose balance below Rs 2000
b) Write a function to accept the account no and amount from the customer
and perform a withdrawal or deposit and print the customer information
along with the new balance.
2. Create a structure employee that holds information like employee code, name,and date of joining and salary of employees. Write a program to create array of
structures and enter data to it. Pass the array to a function and return the
employee information highest paid employee. (Pass array of structures to
function and return structure)
3. Write a function to accept n students record and to return the records of a
student with highest CGPA. Each record has the name, regno, branch and
and CGPA as fields. (Pass array of structures to function and return a record)
4. Write a program to read employee records from a file using structures and
display on the screen. Also display the total count of characters, spaces and
numbers in the file.
5. Write a program to append the contents of one text file at the end of another.
Answer the following questions.
6. Use the following code segment to answer the following questions
-
8/2/2019 Structures Files
2/6
CSE180 Computer Programming in C Lab
Structures & Files
/* mult.c */
int main( int argc , char *argv[] )
{int i , j , w , h ;
if( argc != 3 )
{
printf("usage: %s width height\n",argv[0]);
return 1;
}
w = atoi( argv[1] ) ;
h = atoi( argv[2] ) ;
for( i = 1 ; i
-
8/2/2019 Structures Files
3/6
CSE180 Computer Programming in C Lab
Structures & Files
Suppose this program were executed using the command:
./mul 15w 10h
Which of the following statements are true about the output of the program?
a.There is no output.
b. One hundred fifty numbers are printed.
c. The number at the bottom right of the printout is 150.
d. The thirtieth number printed is 30.
Modify the above program to find the product of n nos.
7. Show the output and answer the questions for the following program.
// start of structure definition
struct Song
{
char Name[25];
float Length;
}; // end of structure definition
// function prototype, receives and returns structure
data type
struct Song ZeroOut(struct Song);
-
8/2/2019 Structures Files
4/6
CSE180 Computer Programming in C Lab
Structures & Files
int main(){
struct Song Title1 = {"White room", 2.50}, Title2;
Title2 = ZeroOut(Title1);
printf("1: %s, %.2f\n", Title1.Name,
Title1.Length);
printf("2: %s, %.2f\n", Title2.Name,
Title2.Length);
}
// beginning of function definition
struct Song ZeroOut(struct Song x)
{
struct Song y = {"Have Mercy On Me", 3.20F};
strcpy_s(x.Name, 25, " ");
x.Length = 0;
return y;
} // end of function definition
a) The structure is not defined inside any function. If it were defined inside the
main() function, as was done in the previous exercises, will this program
work? Why or why not?
b) As shown in the program, the definition of the structure is global, that is, it is
available to all functions. Title1 in main() becomes what variable in
ZeroOut()?
-
8/2/2019 Structures Files
5/6
CSE180 Computer Programming in C Lab
Structures & Files
c) ZeroOut() erased the contents of what was provided through the variable
Title1. Were the contents of Title1 also erased when control went back to
main()?
d) When passing a structure variable to a function, can that function change its
contents?
e) Do functions make copies of structures like they make copies of scalars?
f) Do functions receive only addresses of structures, as they do of arrays?
g) Let us now consider how functions return structures. What is the data type
that ZeroOut() returns?
h) What variable is returned by ZeroOut()? What is its data type? What are its
values?
i) Which variables in main() accepts the returned structure from ZeroOut()?
j) Were the values returned by ZeroOut() received in main()? If so, then into
which variable?
8. Here is the declaration for struct contract; union aptinfo holds information
pertinent to renting, leasing or purchasing an apartment, in that order. The
member how_held, which can have values RENT, LEASE or PURCHASE,
determines which alternative is active.
#define RENT 1
#define LEASE 2
#define PURCHASE 3
struct leaseinfo{
double monthly_lease;
int lease_period;
-
8/2/2019 Structures Files
6/6
CSE180 Computer Programming in C Lab
Structures & Files
};
struct contract{
int how_held;
union aptinfo{
double monthly_rent;
struct leaseinfo lease;
double price;
}monetary;
}apartment;
a) Draw a diagram of the structure apartment, showing the possibilities for
union monetary.
b) How is structure different from union?
c) Write statements to give values to the fields appropriate for leasing the
apartment referred to by structure apartment.
d) Write an if statement that prints whichever fields of union monetary are
active, based on the value of the member how_held.