[go: up one dir, main page]

0% found this document useful (0 votes)
88 views21 pages

ECE131L Week1

Uploaded by

MaharshiGohel
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)
88 views21 pages

ECE131L Week1

Uploaded by

MaharshiGohel
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/ 21

ECE 131L - Programming Fundamentals

Petro M. Tshakwanda, Ph.D.

Fall 2024

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Week 1: Session 1 - Introduction

▶ Course objectives and expectations


▶ Understanding the fundamentals of programming in C
▶ Developing problem-solving skills
▶ Building a foundation for advanced programming concepts
▶ Syllabus overview
▶ Grading policy
▶ Resources
▶ Textbook: ”Programming in C” by Stephen G. Kochan
▶ Online materials and documentation
▶ Class logistics
▶ Communication methods (email, forums)
▶ Office hours and support

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Week 1: Session 1 - Basics of C Programming


▶ History of C programming language
▶ Developed in the 1970s by Dennis Ritchie at Bell Labs
▶ Basis for many modern languages such as C++, Java, and
Python
▶ Importance of C in modern programming
▶ Efficient and powerful
▶ Widely used in systems programming, embedded systems, and
more.
▶ Some Fundamentals:
▶ Computers are essentially dumb machines and operate on a
basic level.
▶ Programming involves expressing solutions in terms of
computer instructions.
▶ Programs consist of instructions to solve specific problems,
known as algorithms.

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Week 1: Session 1 - Basics of C Programming (Cont.)

▶ Some Fundamentals:
▶ Higher-level languages like C provide more sophisticated
operations than assembly languages.
▶ Standardization of syntax in higher-level languages enables
portability across machines.
▶ Compiler translates higher-level language programs into
machine-understandable instructions.

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Programming Languages
▶ Understanding Programming Languages:
▶ Programming languages are essential tools for expressing
algorithms and computations.
▶ They provide abstraction layers over machine instructions for
easier human comprehension and development.
▶ Each programming language has syntax rules and semantics,
defining how instructions are written and interpreted.

Programming Languages
Sequence of 0s and 1s 01010010001110110
Machine Language Directly run on CPU 10010101010101010
Not portable (machine 10110101010101111
dependent)

Symbolic names (English) push %ebp


Assembly Language Need an assembler to translate mov %esp, %ebp
to machine instructions sub $16, %esp
Not portable call getint

#include<stdio.h>
High Level Java, Python, C++, C…
int main(){
Languages Machine Independent
int a = 1,b = 23, sum;
Need a compiler for translation
sum = a+ b;
printf(“total = %d\n”,
sum);
}

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Operating Systems

▶ A program that controls the entire operation of a computer


system.
▶ All IO operations performed on a system are channeled
through OS.
▶ Manages the system’s resources and handles program
execution.
▶ Popular OS systems: Microsoft Windows, MacOS, and Linux.

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Compiler

1. Write your code (edit)


2. Compile the source file (.c)
▶ gcc fileName.c
3. Errors exist? Back to edit
4. No errors?
▶ Translate to machine code (.o)
5. Link all programs (libraries..)
▶ Get final form for execution
6. Executable file (.out/.exe)
▶ Ready to be run

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Compiler (cont.)

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

What is a Computer Program and How to Design It?

▶ A collection of instructions to solve a problem


▶ Designing your program with these steps:
1. Understand the problem
2. Come up with an algorithm (or pseudo-code)
3. Draw a flowchart
4. Write code
5. Test and debug
6. Document your code

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Example:

Example: Getting 3 numbers from a user, find the largest one.


▶ Let’s say a = 3, b = −1, c = 8.
▶ Any idea how to solve this?

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

ALgorithm
Inputs Output

a
Algorithm: a logical way or methods to solve a problem
b max
1. Let assume max = a
2. Compare max to b. Update max if needed c
3. Compare max to c. Update max if needed

And we are Done!

max = 3 8 a = 3, b = -1, c = 8
max > -1
max > 8
Output: max = 8
Week 1: Introduction and Basics of C

Pseudo codes
Pseudo code 1: Pseudo code 2:
Get a,b,c values from Get a,b,c values from user
user if b < a
max = a
if c < a output a
if max < b
then max = b else output c
if max < c else // b >= a
then max = c
if b > c output b
Output max
else output c
Week 1: Introduction and Basics of C

Start

Flow CHART Read a, b, c

Pseudo code: max = a

Get a,b,c values from


No Yes
user max
max
max = b > b? > c?
max = a

if max < b Yes


max
> c?
then max = b
Yes
if max < c

then max = c Output max = b Output max = c Output max = a

End
Week 1: Introduction and Basics of C

Write Code & Document your code


if(max < b)
/*This code finds the biggest
number*/ max = b;
#include<stdio.h> if(max < c)
int main(){ max = c;
//Declaring variables //Output the max #
int a = 3, b = -1, c = 8; printf(“Largest #: %d\n”,
max);
int max = a;
}
Week 1: Introduction and Basics of C

Terminal and Writing your first code


Let’s do some actual command writing using terminal.

Refer to the VIM and LINUX cheat sheet found under Canvas-
>Files

Chapter Notes can be found under Canvas->Files->Chapter Notes


Week 1: Introduction and Basics of C

Week 1: Session 1 - Basics of C Programming (Part 3)

▶ Overview of C development environment


▶ Compiler (e.g., GCC)
▶ Integrated Development Environments (IDEs) (e.g.,
Code::Blocks, Visual Studio)
▶ Structure of a basic C program
▶ Preprocessor commands
▶ The main function
▶ Statements and return value

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Example of a Simple C Program

▶ Example of a simple C Program:


1 # include < stdio .h >
2
3 int main () {
4 printf ( " Hello , World !\ n " ) ;
5 return 0;
6 }

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Week 1: Session 2 - Basic Syntax

▶ Including libraries with #include


▶ Defining the main function
▶ Printing output with printf()

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Example of a Simple C Program

▶ Example of a simple C Program:


1 # include < stdio .h >
2
3 int main () {
4 printf ( " Learning C is fun !\ n " ) ;
5 return 0;
6 }

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Week 1: Session 2 - Data Types and Variables

▶ Understanding data types


▶ int - integer type
▶ float - floating point type
▶ char - character type
▶ Declaring and initializing variables
▶ Syntax: int x = 5;
▶ Variables must be declared before use
▶ Constants and keywords in C
▶ Using const keyword for constants
▶ Reserved keywords (e.g., int, return, if)

Petro M. Tshakwanda, Ph.D.


Week 1: Introduction and Basics of C

Example of a Simple C Program

▶ Example of a simple C Program:


1 # include < stdio .h >
2
3 int main () {
4 int age = 20;
5 float gpa = 3.5;
6 char grade = ’A ’;
7
8 printf ( " Age : % d \ n " , age ) ;
9 printf ( " GPA : %.2 f \ n " , gpa ) ;
10 printf ( " Grade : % c \ n " , grade ) ;
11
12 return 0;
13 }

Petro M. Tshakwanda, Ph.D.

You might also like