Week 1 - Basic Machine Organization and Program Execution
Week 1 - Basic Machine Organization and Program Execution
Computer Organization
Data Hierarchy
These instructions are called programs => We create those using programming languages, e.g.
C++ (more later)
Source: pinterest.ch 5
History of Computers
Alan Turing (1912-1954)
A ‘halting’ machine
6
The Turing Machine
Process
Input Output
https://docs.microsoft.com/en-us/cpp/build/vscpp-step-0-installation?view=msvc-170#visual-
studio-2022-installation
Creating a Visual Studio Project
Configuring Visual Studio for C
1. Right-click the project’s node— —in the and select to display the project’s dialog.
5. In the right column, click in the field to the right of , click the down arrow, then select
7. In the right column, at the end of the value for , insert ;_CRT_SECURE_NO_WARNINGS
9. In the right column, click in the field to the right of , click the down arrow, then select .
Libraries
double z, sum;
x = 7; Initialization
int b = 6;
if (a > b)
printf(a);
else
printf(b);
Selection: Switch Statements
Write A C++ program that takes as an input the letter grade of a student in a course (grade) to compute and display its equivalent numeric
value according to the following rules:
A 4.0
B 3.0
C 2.0
D 1.0
F 0.0
Allow the program to accept the letter grade uppercase or lower case.
Write your program twice, once using nested-if statement and second using switch statement.
Switch Statements – Nested If
//Output the student's GPA based on their letter grade
#include <stdio.h>
int main(void)
{
char grade;
printf("Enter your letter grade\n");
scanf_s("%c", & grade);
//Nested if
if ((grade == 'a') || (grade == 'A'))
printf("4.0\n");
else
if ((grade == 'b') || (grade == 'B'))
printf("3.0\n");
else
if ((grade == 'c') || (grade == 'C'))
printf("2.0\n");
else
if ((grade == 'd') || (grade == 'D'))
printf("1.0\n");
else
if ((grade == 'f') || (grade == 'F'))
printf("0.0\n");
}
Selections: Switch Statement
//switch case 'D’:
switch (grade) printf("1.0\n");
{ break;
case ‘a’: case 'f’:
printf("4.0\n"); printf("0.0\n");
break; break;
case 'A’: case 'F’:
printf("4.0\n"); printf("0.0\n");
break; break;
case 'b’:
printf("3.0\n"); }
break;
case 'B’:
printf("3.0\n");
break;
case 'c’:
printf("2.0\n");
break;
case 'C’:
printf("2.0\n");
break;
case 'd’:
printf("1.0\n");
break;
Iteration: For Loops
Implement the program that counts from 1-100 using a for loop:
printf(“%d\n”, i);
}
Iteration: While Loops versus
For Loops
Write a program that adds the numbers between 1 and 10 inclusive:
int sum = 0;
int n = 1;
while(n <= 10) // add the numbers 1 - 10
{
sum = sum + n;
n++;
}
sum = 0;
for (n = 1; n <= 10; n++) //add the numbers 1 - 10
sum = sum + n;
When to Choose For Loops and
When to Choose While Loops
◦ When there are circumstances for which the loop body should not be executed at all (like when you have to
tell the user to input an even number)
Output all Prime Numbers
between X and Y
/******************************************************************************
#include <stdio.h>
void main()
{
int x, y, flag;
// Iterate to check if i is prime
printf("Enter two numbers:\n");
// or not
scanf_s("%d %d", &x, &y);
for (int j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
// Traverse each number in the
break;
// interval with the help of for loop
}
for (int i = x; i <= y; i++)
}
{
// flag = 1 means i is prime
// Skip 0 and 1 as they are
// and flag = 0 means i is not prime
// neither prime nor composite
if (flag == 1)
if (i == 1 || i == 0)
printf("%d ", i);
continue;
}
// flag variable to tell
}
// if i is prime or not
flag = 1;
Output all Prime Numbers
between X and Y: Output
Arrays
I want to store the grades of all my 70+ students in this class
That is why we use the second type of data structure we learnt (after variables) called arrays.
Arrays are continuous blocks of memory slots booked for the same data type. We can define how
many slots we want and what the data type of all the slots is going to be.
1. Ask user to enter radius of the circle: ‘Please enter radius in meters’
3. Compute the area of the circle following the equation 𝐴 = 𝜋𝑟 2 and store it in a variable
4. Display/output the computed value to the user: ‘The area of the circle is….m2’
Exercise 1
// Area of the circle the circle in meters.\n");
scanf_s("%f", &r);
#define _USE_MATH_DEFINES //We will
discuss these macros in detail //Compute the area
towards the end of the course a = M_PI * pow(r, 2);
}
if (score3 < min)
{
temp = min; //These three assignment statements are what we call a 'swap' operation,
where we swap the values of two variables.
Exercise 3
Write a C program that sorts the elements of an array from smallest to largest.