Week 2 Lecture Code
// Pantea Aria
// if
// Ask the user to enter the number of hours they plan to study today (as an integer).
// If the number is greater than 5, print: "Great job! That's a productive day."
// Otherwise, print: "Try to study a bit more to stay on track."
#include <stdio.h>
#define MAX 5
int main(void) {
printf ("enter the number of hours they plan to study today:");
int hours;
scanf ("%d", &hours);
if (hours > 5) {
printf ("Great job! That's a productive day.\n");
} else {
printf ("Try to study a bit more to stay on track.\n");
}
return 0;
}
// Pantea Aria
// if
// Ask the user to enter their exam score (as a decimal value).
// If the score is 85.0 or higher, print: "Congratulations! You earned a High Distinction."
// Otherwise, print: "Keep working hard to improve your score."
#include <stdio.h>
#define MAX 85.0
int main(void) {
printf ("enter your exam score (as a decimal value)");
double score;
scanf("%lf", &score);
if (score >= MAX) {
printf ("Congratulations! You earned a High Distinction.\n");
} else {
printf ("Keep working hard to improve your score.\n");
}
return 0;
}
// Pantea Aria
// if
// read a letter, and print out "Capital" if it is uppercase
// print out "Lower" if it is a lowercase letter
#include <stdio.h>
int main(void) {
char letter;
printf("Enter a letter:");
scanf (" %c", &letter); //user only enters an alphabet
if (letter >= 'A' && letter <= 'Z') {
printf ("Capital\n");
} else if (letter >= 'a' && letter <= 'z') {
printf ("Lower\n");
} else {
printf("No alphabet\n");
}
return 0;
}
// Pantea Aria
// Your Turn
// if - nested if - logical operators
// Ask the user to enter:
// The total bill amount (as a decimal),
// Whether they have a loyalty card (1 for Yes, 0 for No)
// A user gets a discount if:
// The bill is over $100.00, or
// They have a loyalty card and the bill is over $50.00
// Print whether the user gets a discount or not.
#include <stdio.h>
int main(void) {
// The total bill amount (as a decimal),
double bill;
scanf ("%lf", &bill);
// Whether they have a loyalty card (1 for Yes, 0 for No)
int loyalty_card;
scanf("%d", &loyalty_card);
// A user gets a discount if:
// The bill is over $100.00, or
// They have a loyalty card and the bill is over $50.00
if ( )
return 0;
}
// Pantea Aria
// Your Turn
// if-recap, check if an age is eligible to vote (>=18)
// do this for 10 persons
#include <stdio.h>
#define ELIGIBLE_AGE 18
int main(void) {
return 0;
}
// Pantea Aria
// read 10 ints, find the number of odds
// example: 20, 45, 56, 87, 90, -4, 0, -99,
// print out 3
#include <stdio.h>
#define MAX 10
int main(void) {
// because you know how many times your want to repeat scanf
// you need a loop with a
// declare your counter
int counter = 0;
// declare a variable for your number
int number;
// because you want to count how many odds you have
// you need another counter
// declare a counter for the number of odds
int odds = 0;
printf ("Enter 10 integers: ");
while (counter < MAX) {
scanf ("%d",&number);
// check if that number is odd
if (number % 2 != 0) {
odds = odds + 1;
}
counter = counter + 1;
}
printf ("We have %d odd numbers\n", odds);
return 0;
}
// Pantea Aria
// add all ints from 1 to 100 and print out the result
// 1+2+3+.....+100 = sum
#include <stdio.h>
#define MAX 100
int main(void) {
// because I know how many times I want to repeat summing up
// I should declare a counter for my loop
int number = 1;
// where do I want to keep sum?
// declare summ
int sum = 0;
while (number <= MAX) {
sum = sum + number;
number = number + 1;
}
printf ("Sum is %d\n", sum);
return 0;
}
// Pantea Aria
// You're at an ATM.
// It asks you to enter your 4-digit PIN.
// If it's wrong, it asks again —
// and it will keep asking until you enter the correct one
// am I repeating anything? yes -> loop -> while -> how many times -> we don't know
// if pin is correct you stop
// true or false
// if pin is false I should repeating// pin in this question is called a sentinel value
// imagine the correct PIN is 1234
#include <stdio.h>
#define PIN 1234
int main(void) {
int stop = 0;
int pin;
while (stop != 1) {
printf ("Enter your pin:");
scanf ("%d", &pin);
if (pin == PIN) {
stop = 1;
}
}
return 0;
}
// Pantea Aria
// recap - while
// read numbers until user enters -1, then print out their sum
// what do we need to calculate?
// 10, 30, -5, 90, 34, -1
// sum = (10+30+-5+90+34)
#include <stdio.h>
int main(void) {
int number = 0;
int sum = 0;
while (number != -1) {
scanf ("%d", &number);
sum = sum + number;
}
printf ("Sum is %d\n", sum + 1);
return 0;
}
// Pantea Aria
// nested loops
// 1. Write a C program that prints a 30×40 square of stars.
// 2. The program asks the user for the number of rows and columns,
// then prints row x column rectangle of stars.
#include <stdio.h>
int main(void) {
int row, col;
printf ("Enter row and col:");
scanf ("%d %d", &row, &col);
// print out one row of stars
// if col = 5 you want to print *****
// I want to repeat row times
// declare outer_counter
int outer_counter = 0;
while (outer_counter < row) {
// to declare your inner_counter
int inner_counter = 0;
while (inner_counter < col) {
printf ("*");
inner_counter = inner_counter + 1;
}
printf ("\n");
outer_counter = outer_counter + 1;
}
return 0;
}
// Pantea Aria
// Imagine you are designing a program for a small library system.
// Each book has an ID number (an int),
// a price (a double),
// and a category (a char) that tells you whether the book is
// fiction ('F'),
// non-fiction ('N'),
// or a textbook ('T').
// Write a program to store the information of one book:
// "C Programming Basics", which is a textbook with ID 1091 and costs 59.99.
// define your new type - only declaration
// struct book
struct book {
int id;
double price;
char category;
};
// we have not declared a variable of type struct book yet
#include <stdio.h>
int main(void) {
// declare a variable of type book
struct book new_book, old_book;
// textbook with ID 1091 and costs 59.99.
new_book.id = 1091;
new_book.price = 59.99;
new_book.category = 'T';
// print out double the price of this book
printf("double the price is %lf\n", new_book.price * 2);
}
// Pantea Aria
// Enum
// Imagine you are working in a coffee shop ordering system.
// We can make a type for coffee sizes with limited values, size_type
// (instead of using characters like 'S', 'M', 'L').
//
// enum size_type {SMALL, MEDIUM, LARGE};
// 0 for SMALL
// 1 for MEDIUM
// 2 for LARGE
// Write a program that declares a variable of type size_type
// and assigns it the value LARGE. Then, print the value.
#include <stdio.h>
int main(void) {
}
// Pantea Aria
// Struct and Enum
// Imagine you are designing a program for a small library system.
// Each book has:
// - an ID number (int)
// - a price (double)
// - a category (enum book_type) that can be
// FICTION, NONFICTION, or TEXTBOOK
//
// 0 for FICTION
// 1 for NONFICTION
// 2 for TEXTBOOK
//
// Task:
// Define an enum and a struct to represent a book.
// Then, create a book with ID 101, price 59.99, and category TEXTBOOK.
// Finally, print the book’s details.
// Pantea Aria
// if
// Ask the user to enter the number of hours they plan to study today (as an integer).
// If the number is greater than 5, print: "Great job! That's a productive day."
// Otherwise, print: "Try to study a bit more to stay on track."
#include <stdio.h>
#define MAX 5
int main(void) {
return 0;
}
// Pantea Aria
// if
// Ask the user to enter their exam score (as a decimal value).
// If the score is 85.0 or higher, print: "Congratulations! You earned a High Distinction."
// Otherwise, print: "Keep working hard to improve your score."
#include <stdio.h>
#define MAX 85.0
int main(void) {
return 0;
}
// Pantea Aria
// if
// read a letter, and print out "Capital" if it is uppercase
// print out "Lower" if it is a lowercase letter
#include <stdio.h>
int main(void) {
char letter;
printf("Enter a letter:");
scanf (" %c", &letter); //user only enters an alphabet
return 0;
}
// Pantea Aria
// if - nested if - logical operators
// Ask the user to enter:
// The total bill amount (as a decimal),
// Whether they have a loyalty card (1 for Yes, 0 for No)
// A user gets a discount if:
// The bill is over $100.00, or
// They have a loyalty card and the bill is over $50.00
// Print whether the user gets a discount or not.
#include <stdio.h>
int main(void) {
// The total bill amount (as a decimal),
double bill;
scanf ("%lf", &bill);
// Whether they have a loyalty card (1 for Yes, 0 for No)
int loyalty_card;
scanf("%d", &loyalty_card);
// A user gets a discount if:
// The bill is over $100.00, or
// They have a loyalty card and the bill is over $50.00
if ( )
return 0;
}
// Pantea Aria
// if-recap, check if an age is eligible to vote (>=18)
// do this for 10 persons
#include <stdio.h>
#define ELIGIBLE_AGE 18
int main(void) {
return 0;
}
// Pantea Aria
// read 10 ints, find the number of odds
// 20, 45, 56, 87, 90, -4, 0, -99,
// print out 3
#include <stdio.h>
int main(void) {
return 0;
}
// Pantea Aria
// add all ints from 1 to 100 and print out the result
// 1+2+3+.....+100 = sum
#include <stdio.h>
#define NUM 40
int main(void) {
return 0;
}
// Pantea Aria
// You're at an ATM.
// It asks you to enter your 4-digit PIN.
// If it's wrong, it asks again —
// and it will keep asking until you enter the correct one
// am I repeating anything? yes -> loop -> while -> how many times -> we don't know
// if pin is correct you stop
// true or false
// if pin is false I should repeating// pin in this question is called a sentinel value
#include <stdio.h>
#define PIN 1234
int main(void) {
return 0;
}
// Pantea Aria
// recap - while
// read numbers until user enters -1, then print out their sum
// what do we need to calculate?
// 10, 30, -5, 90, 34, -1
// sum = (10+30+-5+90+34)
#include <stdio.h>
int main(void) {
return 0;
}
// Pantea Aria
// nested loops
// 1. Write a C program that prints a 30×40 square of stars.
// 2. The program asks the user for the number of rows and columns,
// then prints row x column rectangle of stars.
#include <stdio.h>
int main(void) {
return 0;
}
// Pantea Aria
// Imagine you are designing a program for a small library system.
// Each book has an ID number (an int),
// a price (a double),
// and a category (a char) that tells you whether the book is
// fiction ('F'),
// non-fiction ('N'),
// or a textbook ('T').
// Write a program to store the information of one book:
// "C Programming Basics", which is a textbook with ID 1091 and costs 59.99.
// define your new type - only declaration
// struct book
// we have not declared a variable of type struct book yet
#include <stdio.h>
int main(void) {
}
// Pantea Aria
// Enum
// Imagine you are working in a coffee shop ordering system.
// We can make a type for coffee sizes with limited values, size_type
// (instead of using characters like 'S', 'M', 'L').
//
// enum size_type {SMALL, MEDIUM, LARGE};
// 0 for SMALL
// 1 for MEDIUM
// 2 for LARGE
// Write a program that declares a variable of type size_type
// and assigns it the value LARGE. Then, print the value.
#include <stdio.h>
int main(void) {
}
// Pantea Aria
// Struct and Enum
// Imagine you are designing a program for a small library system.
// Each book has:
// - an ID number (int)
// - a price (double)
// - a category (enum book_type) that can be
// FICTION, NONFICTION, or TEXTBOOK
//
// 0 for FICTION
// 1 for NONFICTION
// 2 for TEXTBOOK
//
// Task:
// Define an enum and a struct to represent a book.
// Then, create a book with ID 101, price 59.99, and category TEXTBOOK.
// Finally, print the book’s details.
// Pantea Aria
// read numbers until user enters -1, then print out their average
// what do we need to calculate?
// 10, 30, -5, 90, 34, -1
// sum = (10+30+-5+90+34)
// I let you camculate average yourself = sum/number of inputs
#include <stdio.h>
int main(void) {
return 0;
}
// Pantea Aria
// Your Turn
// read numbers until user enters -1, then print out their average
// what do we need to calculate?
// 10, 30, -5, 90, 34, -1
// sum = (10+30+-5+90+34)
// I let you camculate average yourself = sum/number of inputs
#include <stdio.h>
int main(void) {
return 0;
}