Class 1 - C Language Overview
Class 1 - C Language Overview
In this class we will cover C basics to quickly get to a point where you can write useful
programs: variables, constants, operators, control flow. The best way to learn a new
programming language is by writing programs in it. Therefore, we will use plenty of examples.
#include <stdio.h>
int main()
{
printf("Hello, world!\n");
return 0;
}
Comments:
Example 1.2. Write a program that converts from Fahrenheit to Celcius for temperatures from 0
to 300 in 20-degree increments.
#include <stdio.h>
/*
Print Fahrenheit-Celsius table for 0, 20, ..., 300
*/
int main()
{
for (int i=0; i<=300; i+=20) {
printf("%d\t%d\n", i, 5 * (i-32) / 9);
1
}
}
Answer: the second option. You can specify precision (2 points after decimal) as follows:
printf("%d\t%.2f\n", i, (float) 5 * (i-32) / 9);
We now use symbolic constants to enhance the above program to avoid the use of "magic
numbers", which is considered bad practice.
#include <stdio.h>
/*
Print Fahrenheit-Celsius table
*/
int main()
{
printf("%s\n", "Fahrenheit to Celsius");
for (float temp=LOWER; temp<=UPPER; temp+=STEP) {
printf("%.2f\t%.2f\n", temp, 5 * (temp-32) / 9);
}
Example 1.3. Write a program that outputs the number of lines in a file.
#include <stdio.h>
2
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == EOL)
++nl;
printf("%d\n", nl);
return 0;
}
Comments:
Example 1.4. Future value (FV) is the value of a current asset at a future date based on an
assumed rate of growth. One example is putting money into a certificate of deposit (CD) for a
certain number of years to earn a simple interest paid annually. In such cases the FV formula is:
𝐹𝑉 = 𝐼 × (1 + (𝑅 × 𝑇))
where:
● I = Investment amount
● R = Interest rate
● T = Number of years
For example, assume a $1,000 investment is held for 5 years in a CD account with 10% simple
interest paid annually. In this case:
𝐹𝑉 = $1000 × (1 + 0. 1 × 3) = $1300
We write a program that computes future value given the above parameters.
#include <stdio.h>
3
int main()
{
double amount, rate;
int years;
scanf("%lf", &amount);
scanf("%lf", &rate);
scanf("%d", &years);
printf("FV=%.2lf for amount=%.2lf, rate=%.2lf, years=%d\n", fv(amount,
rate, years), amount, rate, years);
return 0;
}
Comments:
Why do you think a function's definition is needed before main (choose the best answer)?
Answer: the first answer - a compiler needs to know about the function's signature before it's
used. Other answers are incomplete or incorrect.
4
Example 1.5. With simple interest, it is assumed that the interest rate is earned only on the initial
investment. With compounded interest, the rate is applied to each period's cumulative account
balance. For example, investing $1,000 into a savings account with 10% compounded interest
for 3 years will work as follows.
● Year 1
○ $1000 × 1. 1 = $1100
● Year 2
○ $1100 × 1. 1 = $1210
○ Note: the new balance (investment amount)
● Year 3
○ $1210 × 1. 1 = $1331
○ This is the future value using compounded interest
𝑇
𝐹𝑉 = 𝐼 × (1 + 𝑅)
where:
● I = Investment amount
● R = Interest rate
● T = Number of years
#include <stdio.h>
#include <math.h>
int main() {
float amount, rate;
unsigned int years;
printf("Enter amount, rate, years: ");
scanf("%f", & amount);
scanf("%f", & rate);
scanf("%u", & years);
printf("FV=$%.2f for amount=$%.2f, rate=%.2f per cent, years=%d\n",
fv(amount, rate / 100, years), amount, rate, years);
return 0;
}
5
Comments:
You invest $x for one year earning 10% interest. In one year you receive $100. What is x closest
to?
● $110
● $89
● $101
● $91
Example 1.6. Write a program that computes future value of multiple yearly investments. For
example, an investor is planning to allocate predefined amounts into a savings account.
#include <stdio.h>
#include <math.h>
#define MAX_YEARS 10
int main() {
float amount[MAX_YEARS], rate[MAX_YEARS];
unsigned int num_years;
float total_fv = 0;
printf("Enter number of years for investments: ");
scanf("%u", & num_years);
for (int year = 0; year < num_years; year++) {
printf("Enter amount, rate in year %d: ", year);
scanf("%f", & amount[year]);
scanf("%f", & rate[year]);
total_fv += fv(amount[year], rate[year] / 100, num_years - year);
}
printf("FV=$%.2f\n", total_fv);
return 0;
6
}
Comments:
● Yes
● No
Answer: No. Even though we store input numbers in arrays, only one value is used at a time,
which can be a simple variable.
Program:
#include <stdio.h>
int main() {
1
Hal Greenwald, CS531 Lecture Notes, Week 1
7
int num_rows, temp;
printf("Enter the number of rows in pyramid:");
scanf("%d", & num_rows);
printf("\n");
for (int row = 1; row <= num_rows; row++) {
for (int space = 1; space <= num_rows - row; space++)
printf(" ");
for (int star = 1; star <= 2 * row - 1; star++)
printf("*");
printf("\n");
}
return 0;
}
Output:
*
***
*****
Comments:
Trace:
1 2 1
2 1 3
3 0 5
8
#include <stdio.h>
int main() {
int num_rows, temp;
printf("Enter the number of rows in pyramid:");
scanf("%d", & num_rows);
printf("\n");
for (int row = 1; row <= num_rows; row++) {
for (int space = 1; space <= num_rows - row - 1; space++)
printf(" ");
for (int star = 1; star <= 2 * row - 1; star++)
printf("*");
printf("\n");
}
return 0;
}
Exercise 1.1 (20 minutes). Modify the pyramid code to display it upside down.
#include <stdio.h>
int main() {
int num_rows, temp;
printf("Enter the number of rows in pyramid:");
scanf("%d", & num_rows);
printf("\n");
for (int row = num_rows; row >= 1; row--) {
for (int space = 1; space <= num_rows - row; space++)
printf(" ");
for (int star = 1; star <= 2 * row - 1; star++)
printf("*");
printf("\n");
}
return 0;
}
9
10