0% found this document useful (0 votes)
27 views

FOC Unit 1B slides

Uploaded by

abishekcr5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

FOC Unit 1B slides

Uploaded by

abishekcr5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

M.S.

Ramaiah Institute of Technology


(Autonomous Institute, Affiliated to VTU)
Department of Computer Science and Engineering

Course Name: Fundamentals of Computing


Course Code: CS16
Credits: 2:0:0
UNIT 1

Term: Odd Semester 2021

Faculty:
Mrs.Darshana A Naik
Mrs.Shilpa H
Mrs Sunitha R S

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


UNIT 1 Syllabus Covered
Overview of C: Basic Structure of C Programs. Constants, Variables, and Data
types: Character Set, C-Tokens, Keywords and Identifies, Constants, Variables,
Data Types, Defining Symbolic Constants. Managing Input and Output
Operations: Reading a Character, writing a Character, Formatted Input
Formatted Output

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 2


Program Structure

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


3
Program Structure

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 26


Some Key Terms

◼ Source Program
◼ printable/Readable Program file
◼ Object Program
◼ nonprintable machine readable file
◼ Executable Program
◼ nonprintable executable code
◼ Syntax errors
◼ reported by the compiler
◼ Linker errors
◼ reported by the linker
◼ Execution/Run-time errors
◼ reported by the operating system

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


5
Executing a Computer Program

◼ Compiler
◼ Converts source program to object program
◼ Linker
◼ Converts object program to executable program

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


6
General Form
preprocessing directives ◼ The main function contains two types
of commands: declarations and
int main(void)
statements
{
◼ Declarations and statements are
declarations statements required to end with a semicolon (;)
} ◼ Preprocessor directives do not end with
a semicolon
◼ To exit the program, use a

return 0; statement

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


28
Process of Compiling and executing
C program

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 8


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 9
Another Program
/***************************************************/
/* Program chapter1 */
/* This program computes the sum of two numbers */
#include <stdio.h> int main(void)
{
/* Declare and initialize variables. */
double number1 = 473.91, number2 = 45.7, sum;
/* Calculate sum. */
sum = number1 + number2;
/* Print the sum. */
printf(“The sum is %f \n”, sum);

return 0; /* Exit program. */


}
/****************************************************/

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


29
/* */ Comments Preprocessor,
/* Program chapter1_1 */
/* This program computes the */ standard C library
/* distance between two points. */
every C program must have
#include <stdio.h> #include <math.h> main function, this one takes no
parameters and it returns int
int main(void) value
{ { → begin
/* Declare and initialize variables. */
double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance; Variable declarations, initial
values (if any)
/* Compute sides of a right triangle. */
Statements
must end with ;
side_1 = x2 - x1; side_2 = y2 - y1;
indentation
distance=sqrt(side_1*side_1 + side_2*side_2);

/* Print distance. */ indentation


printf("The distance between the two " "points is
indentation
%f \n", distance);
return 0
return 0; /* Exit program. */ } → end of function
}
27
/* */
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Constants and Variables
◼ What is a variable in math? f(x) = x2+x+4
◼ In C,
◼ A variable is a memory location that holds a value

◼ An identifier or variable name is used to reference a


memory location.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


30
Memory
double x1=1,x2=7,distance;
name address Memory - content

11
How many memory cells
x1 12 1 = 00000001 does your computer have?
Say it says 2Gbyte memory?
x2 13 7 = 00000111
1K=103 or 210 = 1024
1M=106 or 220 =10242
14
1G=109 or 230 =10243
distance 15
? = 01001101

16
… DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
31
Memory Snapshot

Name Addr Content


x1 1
y1 5
x2 4
y2 7
side_1 ?
side_2 ?
distance ?

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 14


Rules for selecting a valid
identifier (variable name)
◼ Must begin with an alphabetic character or underscore
(e.g., abcABC_)
◼ May contain only letters, digits and underscore (no
special characters ^%@)
◼ Case sensitive (AbC, aBc are different)
◼ Cannot use C keywords as identifiers (e.g., if, case,
while)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 15


Are the following valid identifiers?
◼ distance ◼ rate% ◼ initial_time
◼ 1x ◼ x_sum ◼ DisTaNce

◼ x_1 ◼ switch ◼ X&Y

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 16


C Numeric Data Types

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 17


Example Data-Type Limits

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 18


C Character Data Type: char
char result =‘Y’;
In memory, everything is stored as binary value, which can be interpreted as char or integer.
Examples of ASCII Codes

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 19


Memory
name address Memory - content
How to represent ‘a’ ?
char My_letter=‘a’; 0
int My_number = 97
My_letter 1 ‘a’= 01100001
Always we have 1’s and 0’s in the 97 = 01100001
memory. It depends on how you look My_number 2
at it?
For example, 01100001 is 97 if you 3
look at it as int, or
4
‘a’ if you look at it as char
? = 01001101
5
‘3’ is not the same as 3 How to
6
represent 2.5?

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
38
Constants
◼ A constant is a specific value that we use in our programs. For
example
3.14, 97, ‘a’, or “hello”
◼ In your program,
int a = 97; a
char b =‘a’;
01100001
double area, r=2.0; 01100001 b
double circumference;
? area
area = 3.14 * r*r;
circumf
circumference = 2 * 3.14 * r; ? erence

2.0 r
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 21
Symbolic Constants
◼ What if you want to use a better estimate of ?
For example, you want 3.141593 instead of 3.14.
◼ You need to replace all by hand
◼ Better solution, define  as a symbolic constant, e.g.
#define PI 3.141593

area = PI * r * r;
circumference = 2 * PI * r;
◼ Defined with a preprocessor directive
◼ Compiler replaces each occurrence of the directive identifier with the
constant value in all statements that follow the directive

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 22


Assignment Statements
◼ Used to assign a value to a variable
◼ General Form:
identifier = expression;
/* ‘=‘ means assign expression to identifier */
◼ Example 1
double sum = 0; 0 sum
Example 2 int x; x=5; x

5
◼ Example 3
char ch;
ch = ‘a’;
‘a’ ch

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23


Assignment examples (cont’d)
◼ Example 3
int x, y, z; x
x = y = 0;
0
y
right to left! 0
z
Z = 1+1; 2

◼ Example 4
y=z;
y=5;

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 24


Assignment examples with
different types
int a, b=5; ? 2 a
double c=2.3; 5 b
… a=c; c=b;
2.3 5.0 c

long double, double, float, long integer, integer, short integer, char
→ Data may be lost. Be careful!
 No data loss

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


44
Exercise: swap
First Attempt
x=y; y=x;

x 3 x 5 x 5
y 5 y 5 y 5

Before After x=y After y=x

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


45
Exercise: swap
Solution
temp= x;
x=y; y=temp;

x x x x 5
3 3 5
y y y y 3
5 5 5
temp temp temp temp 3
? 3 3

Before after temp=x after x=y after y = temp


Will the following solution work, too? temp= y;
y=x;
x=temp;
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
46
Standard Input and Output
◼ Output: printf
◼ Input: scanf
◼ Remember the program computing the distance between two points!
/* Declare and initialize variables. */
double x1=1, y1=5, x2=4, y2=7, side_1, side_2,
distance;
◼ How can we compute distance for different points?
◼ It would be better to get new points from user, right? For this we will use
scanf
◼ To use these functions, we need to use
#include <stdio.h>

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 28


Standard Output
◼ printf Function
◼ prints information to the screen
◼ requires two arguments
◼ control string
Access
◼ Contains text, conversion specifiers or both
Specifier
Identifier to be printed

◼ Example Control String


double angle = 45.5;
printf(“Angle = %.2f degrees \n”, angle);

Output:
Angle = 45.50 degrees

Identifier
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 29
Conversion Specifiers for Output
Statements

Frequently Used

30
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Standard Output
Output of 157.8926
Specifier Value Printed
%f 157.892600
%6.2f 157.89
%7.3f 157.893
%7.4f 157.8926
%7.5f 157.89260
%e 1.578926e+02
%.3E 1.579E+02
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 31
Exercise
int sum = 65;
double average = 12.368; char ch = ‘b’;

Show the output line (or lines) generated by the following statements.

printf("Sum = %5i; Average = %7.1f \n", sum, average);


printf("Sum = %4i \n Average = %8.4f \n", sum, average);
printf("Sum and Average \n\n %d %.1f \n", sum, average);

printf("Character is %c; Sum is %c \n", ch, sum);


printf("Character is %i; Sum is %i \n", ch, sum);

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 32


Exercise (cont’d)
◼ Solution
Sum = 65; Average = 12.4
Sum = 65
Average = 12.3680 Sum and
Average

65 12.4
Character is b; Sum is A
Character is 98; Sum is 65

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 33


Standard Input
◼ scanf Function
◼ inputs values from the keyboard
◼ required arguments
◼ control string
◼ memory locations that correspond to the specifiers in the

control string
◼ Example:
int distance; char unit_length;
scanf("%lf %c", &distance, &unit_length);
 It is very important to use a specifier that is appropriate for the data
type of the variable

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 34


Conversion Specifiers for Input
Statements

Frequently Used

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 35


Exercise
float f; int i;
scanf(“%f %d“, &f, &i);

◼What will be the values stored in f and i after scanf statement if


following values are entered
12.5 1

12 45
12 23.2
12.1 10
12
1

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 36


Good practice
◼You don’t need to have a printf before scanf, but it is
good to let user know what to enter:
printf(“Enter x y : ”);
scanf(“%d %d”, &x, &y);
◼ Otherwise, user will not know what to do!
◼ What will happen if you forget & before the variable
name?

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 37


Exercise: How to input two points without re-
compiling the program

printf(“enter x1 y1: “); scanf(“%lf %lf“,


&x1, &y1); printf(“enter x2 y2: “);
scanf(“%lf %lf“, &x2, &y2);

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 38


Programming exercise
◼Write a program that asks user to enter values for
the double variables (a, b, c, d) in the following
formula. It then computes the result (res) and prints it
with three digits after .

a + b a + c c +b c − d
res = +
a−b a +c
39
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Math Functions
#include <math.h>
fabs(x) Absolute value of x.

sqrt( x) Square root of x, where x>=0.

pow(x,y) Exponentiation, xy. Errors occur if


x=0 and y<=0, or if x<0 and y is not an integer.
ceil(x) Rounds x to the nearest integer toward  (infinity). Example, ceil( 2 .
01 ) is equal to 3.
floor( x) Rounds x to the nearest integer toward - (negative infinity).
Example, f loor( 2 . 01 ) is equal to 2.
exp(x) Computes the value of ex.
log(x) Returns ln x, the natural logarithm of x to the base e.
Errors occur if x<=0.
log10(x) Returns log10x, logarithm of x to the base 10.
Errors occur if x<=0.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 40


Trigonometric Functions

si n( x) Computes the sine of x, where x is in radians. Computes the


cos( x) cosine of x, where x is in radians Computes the tangent of x,
tan(x) where x is in radians. Computes the arcsine or inverse sine of x,
asin(x) where x must be in the range [-1, 1].
Returns an angle in radians in the range [-/2,/2].

acos(x) Computes the arccosine or inverse cosine of x,


where x must be in the range [-1, 1].
Returns an angle in radians in the range [0, ].
atan(x) Computes the arctangent or inverse tangent of x. The
Returns an angle in radians in the range [-/2,/2].
atan2(y,x) Computes the arctangent or inverse tangent of the value y/x. Returns an angle in
radians in the range [-, ].

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 41


Parameters or Arguments of a
function
◼ A function may contain no argument or contain one
or more arguments
◼ If more than one argument, list the arguments in the correct order

◼ Be careful about the meaning of an argument. For example, sin(x)


assumes that x is given in radians, so to compute the sin of 60 degree,
you need to first conver 60 degree into radian then call sin function:
#define PI 3.141593
theta = 60;
theta_rad = theata * PI / 180;
b = sin(theta_rad); /* is not the same as sin(theta); */

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 42


Exercise
◼ Write an expression to compute velocity using the following equation
◼ Assume that the variables are declared

velocity = vo2 + 2a(x − xo)

velocity = sqrt(vo*vo+2*a*(x-xo));

velocity = sqrt(pow(vo,2)+2*a*(x-xo));

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 43


Character Functions
#include <ctype.h>

putchar(‘a’);
C= getchar();

toupper(ch) If ch is a lowercase letter, this function returns the corresponding uppercase


letter; otherwise, it returns ch Returns a nonzero value if ch is a decimal digit;
isdigit(ch) otherwise, it returns a zero.
Returns a nonzero value if ch is a lowercase letter; otherwise, it returns a zero.
islower(ch) Returns a nonzero value if ch is an uppercase letter; otherwise, it returns a zero.
Returns a nonzero value if ch is an uppercase letter or a lowercase letter;
isupper(ch) otherwise, it returns a zero.
Returns a nonzero value if ch is an alphabetic character or a numeric digit;
isalpha(ch) otherwise, it returns a zero.

44
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Exercise
What is the output of the following program

#include <stdio.h> #include <ctype.h>


int main(void)
{

char ch1='a', ch2; char


ch3='X', ch4;
char ch5='8';
ch2 = toupper(ch1); printf("%c %c
\n",ch1,ch2); ch4 = tolower(ch3);
printf("%c %c \n",ch3,ch4);
printf("%d\n",isdigit(ch5));
printf("%d\n",islower(ch1));
printf("%d\n",isalpha(ch5));
return(0);
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 45


Exercise Output
What is the output of the following program

#include <stdio.h> #include <ctype.h>

int main(void)
{
char ch1='a', ch2; char
ch3='X', ch4;
char ch5='8';
ch2 = toupper(ch1);
printf("%c %c \n",ch1,ch2); aA

ch4 = tolower(ch3); Xx
printf("%c %c \n",ch3,ch4); 2048
printf("%d\n",isdigit(ch5)); 512
printf("%d\n",islower(ch1)); 0
printf("%d\n",isalpha(ch5));
return(0);
46
} DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Summary
◼ Computing systems need a specific plan to design a solution for a
problem.
◼ The steps to engineering problem solving methodology are:
◼ Problem statement.
◼ Describe the input and output.
◼ Work the solution by hand with simple set of data.
◼ Design a solution and convert it into a program.
◼ Test the solution.
◼ Internal organization of a computer.
◼ Linking and loading process.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


47
Thank you

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 48

You might also like