You are on page 1of 4

Department of Information Systems and Technologies

CTIS151 – Introduction to Programming


Spring 2021- 2022
Lab Guide #12 – Week 11 – 1

OBJECTIVES : Functions with output parameters + File operations

Instructor : Burcu LIMAN


Assistants : Burcu ALPER & Leyla SEZER

Q1. Fill in the blanks in the following C program using the given addresses and values of ptr and y variables. Then, write and
execute it on your development environment and observe if all of the output is valid or not to correct your errors;
addresses should look like hexadecimal numbers while values should look like decimal values.
#include <stdio.h>
ptr y
Value: 0055F760
*
Value: 860
int main (void) &
{
int y = 860; Address: 0055F754 Address: 0055F760
int *ptr;
ptr = &y;
printf("1.Value of the variable y by using y is : __\n", ___);
printf("2.Value of the variable y by using ptr is : __\n", ___);

printf("\n1.Address of the variable y by using y is : __\n", ___);


printf("2.Address of the variable y by using ptr is : __\n", ___);

printf("\n1.Address of the pointer ptr by using ptr is : __\n", ___);

printf("\n1.Value of the pointer ptr by using ptr is : __\n", ___);

*ptr = 205;

printf("\n1.Value of the variable y by using y is : __\n", ___); Project Name: LG12_Q1


printf("2.Value of the variable y by using ptr is : __\n", ___); File Name: Q1.cpp
return 0;
}

Q2. In a Car Credit system, there is a calculator for the customers that calculates monthly payment and total money amount
for a customer which wants to receive a loan for a car, according to the selected bank information.

Write the following functions;


 menu() that shows the menu as in the example run and gets the bank choice of the user. Function also makes
the data validation for the user’s choice. Then returns the choice to the main part.
 calculatePayAndTotal() that gets the credit amount, maturity of the user and interest rate of the bank,
then function calculates the total amount which customer will pay to the bank and monthly pay plan.
Write a C program that shows the bank menu for the customer who wants to take a credit for a car. Each bank’s interest
rate is different;
 VAKIFBANK -> 1.34, Project Name: LG12_Q2
 YAPIKREDI ->1.55, File Name: Q2.cpp
 GARANTI -> 1.69,
 SEKERBANK ->1.51
Program gets the credit amount, maturity and the bank selection of the customer. Then, program calculates the total
money which customer will be pay for the bank and monthly payment according to the given maturity, using bank
interest rates.

Example Run:
Enter your credit amount: 180000 4. SEKERBANK
Enter your maturity: 60 Select one of the bank: 9
Select one of the bank: -3
MENU Select one of the bank: 3
1. Vakifbank
2. YAPI KREDI Total money you will pay is 304200.00
3. GARANTI Monthly pay is 5070.00
Q3. In a central phone company; for the 4 big cities in Turkey (Ankara, Istanbul, Izmir and Konya); new phone numbers will be
generated according to the city code and old phone numbers of a user.

Write the following functions;


 numOfDigit() that gets an integer number and returns the number of digits .
 findCity() that gets the city code ,phone number and returns the plate number and new phone number.

Write a C program that gets city code and phone number of 3 users, then it makes the new phone and also shows the plate
code according given conditions and using the following functions.
 Plate number is; 06 for city code 312 (Ankara),
 Plate number is; 34 for city code 212 (Istanbul),
 Plate number is; 35 for city code 232 (Izmir),
 Plate number is; 42 for city code 332 (Konya),
 Otherwise, assume plate number is 0.

New Phone number will be generated; plate number * city code, then it is added to the old phone number.
Your program should make data validation for the digits of the city code (312 must be 3 digit) and also phone number (290
5058 must be 7 digit).

ExampleRun:
Enter the 1. user city code and phone number:25 6394125 Project Name: LG12_Q3
File Name: Q3.cpp
Wrong city code or phone enter again!
Enter 1. user the city code and phone number:212 5653987

1. user Plate Code is 34 and new phone number is 5661195

Enter the 2. user city code and phone number:312 9654787


2. user Plate Code is 6 and new phone number is 9656659

Enter the 3. user city code and phone number:232 569874

Wrong city code or phone enter again!


Enter 3. user the city code and phone number:232 9653614

3. user Plate Code is 35 and new phone number is 9661734

Q4.
a) Write a C program that reads 10 numbers from a text file named “numbers.txt”. Then, it displays these values on the
screen and calculate their multiplication.

numbers.txt
4 6 9 1 2 8 3 5 7 1 3 9 6 3 6 8 9 3 Project Name: LG12_Q4a
File Name: Q4a.cpp
Example Run:
Numbers read from the file are: 4 6 9 1 2 8 3 5 7 1
The multiplication of these numbers 362880

b) Modify the program Q4a that the file is, now, read until the end of file, instead reading a certain amount of numbers.

Example Run:
Numbers read from the file were: 4 6 9 1 2 8 3 5 7 1 3 9 6 3 6 8 9 3 Project Name: LG12_Q4b
The multiplication of these numbers were 928878592 File Name: Q4b.cpp

c) Modify the program Q4b that reads the numbers from the file “numbers.txt”. Then, writes the numbers which are even
to the even.txt, writes other numbers to the file named odd.txt.

even.txt
4 6 2 8 6 6 8
Project Name: LG12_Q4c
odd.txt File Name: Q4c.cpp
9 1 3 5 7 1 3 9 3 9 3
Q5. Write the following function;
 isPerfect() that gets an integer number then decides whether an integer is perfect or not.
( A perfect number is a number whose sum of it is divisors equal to number itself excluding number itself and 1. )

Write a C program that reads a list of integers from a text file named “integers.txt” and displays the perfect numbers.

integers.txt
6 10 38 91 65 99 32 67 80 91 28 15 18 23 37 45 86 72 41 62 38 93 27 68 90 12 25 80 33 57

Example Run:
Project Name: LG12_Q5
Perfect numbers are : 6 28
File Name: Q5.cpp
Additional Questions
AQ1.
Write the following function;
 convertDays() that gets the value the user gave in units of days, in order to compute and “return” the
respective values of years, months and days.
Write C program that gets a number representing an amount of days for this number in the units of days to be represented
in years, months and days respectively. Examine the example run below, carefully.

Hint: Assume that one year is 365 days, one month is 30 days.

Example Run:
Enter the number of days (-1 to stop): 3333
3333 days is 9 year(s), 1 month(s), 18 day(s)

Enter the number of days (-1 to stop): 365


365 days is 1 year(s), 0 month(s), 0 day(s)

Enter the number of days (-1 to stop): 30


30 days is 0 year(s), 1 month(s), 0 day(s)

Enter the number of days (-1 to stop): 1


1 days is 0 year(s), 0 month(s), 1 day(s)

Enter the number of days (-1 to stop): -1


Project Name: LG12_AQ1
File Name: AQ1.cpp

AQ2.
Write the following functions;
 convertUnits() that gets the parameters of hours, minutes, seconds and milliseconds and calculates how many
minutes, seconds and milliseconds have been passed to beat that record.
 aDayLater() that gets the parameters of hours, minutes, seconds and milliseconds and calculates how many
minutes, seconds and milliseconds will have been passed to beat that record after 1 day passed.
Write a C program that gets a Guinness World Record in hours:minutes:seconds.milliseconds format (until a negative value
for any one of those is entered) and finds minutes, seconds and milliseconds passed to beat that record.

Example Run:
Enter a guiness record in the given format (hours:minutes:seconds:milliseconds): 1:23:45.67

There are 83 minutes, 5025 seconds and 502567 milliseconds.


One day later, there are 1523 minutes, 91425 seconds and 9142567 milliseconds.

Enter a guiness record in the given format (hours:minutes:seconds:milliseconds): 2:34:56.78

There are 154 minutes, 9296 seconds and 929678 milliseconds.


One day later, there are 1594 minutes, 95696 seconds and 9569678 milliseconds.

Enter a guiness record in the given format (hours:minutes:seconds:milliseconds): -1:11:11.11

Project Name: LG12_AQ2


File Name: AQ2.cpp

You might also like