/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

 #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
CECS 130 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CECS 121 Final Test.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data.
CECS 130 Mid-term Test Review Provided by REACH – Resources for Academic Achievement Presenter: [REACH Tutor]
PGT C Programming1 Week 4 – Repetition Structures / Loops.
CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Lecture2.
Chapter 1.2 Introduction to C++ Programming
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
ECE Application Programming
Lecture 7: Repeating a Known Number of Times
Yanal Alahmad Java Workshop Yanal Alahmad
Basic Elements of C++.
Quiz 11/15/16 – C functions, arrays and strings
Chapter 4 - Program Control
CECS 130 Mid-term Test Review
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Getting Started with C.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
Looping.
Arrays, For loop While loop Do while loop
Chapter 4 - Program Control
Program Control Topics While loop For loop Switch statement
Lecture3.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Chapter 4 - Program Control
DATA TYPES There are four basic data types associated with variables:
CECS 130 Mid-term Test Review
Getting Started With Coding
Presentation transcript:

/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }

Serve to control program execution and functionality. Must end with a semicolon(;) with the exception of:  Comments: /* */  Preprocessor Directives: #include or #define  Begin and end program identifiers: { }  Function definition beginnings: main()

 Functions allow you to group program statements under one name  C is case-sensitive so main(), MAIN(), and Main() are all different  The main function is special because the values it returns are returned to the operating system  Most main functions in this course do not take or pass information to the operating system

 Definition: Escape sequences are specially sequenced characters used to format output  \”  Ex: printf(“ \ “This is quoted text \ “ “)  \’  Ex: printf(“ \n A single quote looks like \’ \n”);  \* *\ Comment Block

 #include  Using a directive to include a header file  stdio.h = standard input output header file  stdlib.h = ‘system’ commands

 A computer’s long-term memory is called nonvolatile memory and is generally associated with mass storage devices, such as hard drives.  A computer’s short term memory is called volatile memory. It loses is data when power is removed from the computer

To declare a constant (read only) value: const int x = 20 const float PI = 3.14

TYPESIZEVALUES bool1 bytetrue (1) or false (0) char1 byte‘a’ to‘z’, ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on int4 bytes-2,147,483,648 to 2,147,483,647 short2 bytes-32,768 to 32,767 long4 bytes-2,147,483,648 to 2,147,483,647 float4 bytes+ - (1.2 x 10^-38 to 3.4 x 10^38) double8 bytes+- (2.3 x 10^-308 to -1.7 x 10^308)

 Can you explain what the code is doing?

 Character - %c  Integer - %d  Float (decimal)- %f  String - %s  Printf Format Tags: %[flags][width][.precision][length]specifier %[.precision]specifer -> %.2f

 int main() { printf (“%c %c \n", 'a', 65); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", ); printf ("%s \n", "A string"); printf(“%f \n”, 55.55); return 0; } }

printf (“%c %c \n", 'a', 65); aA printf (" %10d \n", 1977); 1977 printf ("%010d \n", 1977); printf ("floats: %4.2f \n", ); 3.14 printf ("%s \n", "A string"); A string printf(“%f \n”, 55.55);

 Syntax scanf(“conversion specifier”, variable);

#include main() { int iOperand1 = 0; int iOperand2 = 0; printf(“\n Enter first operand: “); scanf(“%d”, &iOperand1); printf(“\n Enter second operand: “); scanf(“%d”, &iOperand2); printf(“The result is %d \n”, 24/(iOperand1 * iOperand2)+6/3); }

#include main() { int x = 4; int y = 9; int result1, result2; result1 = y/x; result2 = y%x; printf(“The result is %d.%d \n”, result1, 25*result2); }

Do you know the answers to these?  A. !( 1 || 0 )  B. !( 1 || 1 && 0 )  C. !( ( 1 || 0 ) && 0 )

 A. !( 1 || 0 ) ANSWER: 0  B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)  C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

 Can you write code that will ask a user to enter a number 1, 2, or 3 and print out the following:

#include int main() { int a; printf (“Enter one of the following: %d, %d, or %d\n”, 1, 2, 3); scanf(“%d”, &a); if(a==1|| a==2|| a ==3) { if(a==1){ printf(“\n %d is the loneliest number \n“, 1); } if(a==2){ printf(“\n%d is better than %d \n”,2,1); } if(a==3){ printf(“\n%d \’ s a crowd \n”,3); } else printf(“Sorry, you entered an invalid value\n”); return 0; }

 while ( condition ) { Code to execute while the condition is true }  Quiz: Can you write a program that prints x while x increments from 0 to 10?

 Syntax:  do { //Code executed while condition is true } while ( condition );  Question: What is the main difference between the “do-while” and “while” functions?

 Do-while executes code at least once!

x++;Tells the function to use the current value of x and increment it by 1. ++x;Increment the value of x by 1 and use the new value for calculations. x--; Tells the function to use the current value of x and decrease its value by 1. --x; Decrease the value of x by 1 and use the new value for calculations. x=0; printf(“The Value of x is: %d”, x++); printf(“\n The Value of x is: %d”,++x); Would results in:The Value of x is: 0 The Value of x is: 2

 Often used when the # of iterations is already known.  Contains 3 separate expressions: 1. Variable initialization 2. Conditional expression 3. Increment/Decrement Try writing a program with a for loop that counts down from 10 seconds.

#include main() { int x=0; for(x=10; x>=0; x--) { printf("%d \n", x); } system("pause"); }

 break; Used to exit a loop. Once this statement is executed the program will execute the statement immediately following the end of the loop.  continue; Used to manipulate program flow in a loop. When executed, any remaining statements in the loop will be skipped and the next iteration of the loop will begin

 Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN )  Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters  Function definitions implement the function prototype  Where are function prototypes located in the program?  Where do you find function definitions?

 Where are function prototypes located in the program?  Answer: before the main(){} Function!  Function Definitions are self contained outside of the main(){} function

#include int mult ( int, int); int main() { int x; int y; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n", mult( x, y ) ); getchar(); } int mult (int a, int b) {return a * b; }

#include printNumber(); main() { int x; printNumber(x); } printNumber() { printf(“\n The number is %d \n”, x) }

#include void printNumber( int x); main() { int x; printNumber(x); } void printNumber(int y) { printf(“\n The number is %d \n”, y); } *Note: it’s not absolutely necessary to write VOID, but it’s a good practice.

#include void printNumbers(); int iNumber; main() { int x; for(x=0, x<10,x++){ printf(“\n Enter a number:”); scanf(“%d”, &iNumber); printNumbers(); } void printNumbers() { printf(“\n Your number is: %d \n”, iNumber); }

 Variable scope defines the life time of a variable  Local Scope: defined within functions and loses scope after function is finished. Can reuse in other functions (ex. p.123)  Global Scope: defined outside of functions and can be accessed by multiple functions

 Can you write code that asks a user to input 4 integers, adds the numbers together in one function, multiplies them in another, and finally prints out the difference between the sum and product? The user should have to do this 5 times.

#include int addNumbers(int, int, int, int); int multNumbers(int, int, int, int); main() { int a, b, c, d, counter=0; while(counter<6){ printf(“\n Please enter 4 integers separated by spaces\n”); scanf(“%d %d %d %d”, &a, &b, &c, &d); printf(“\n %d \n”, addNumbers(a,b,c,d)-multNumbers(a,b,c,d)); count++; } int addNumbers(int f, int g, int h, int y) { return f+g+h+y; } int multNumbers(int f, int g, int h, int y) { return f*g*h*y; }

 Can you declare a one-dimensional array made up of 10 integers?  Answer: int iArray[10]  How to declare an Array  int iArray[10];  float fAverages[30];  double dResults[3];  short sSalaries [9];  char cName[19]; //18 characters and 1 null character

 Why do we initialize? Because memory spaces may not be cleared from previous values when arrays are created  Can initialize an array directly  Example int iArray[5]={0,1,2,3,4};  Can initialize an array with a loop such as FOR()

#include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; }

 Can you add code to print out the values of the program below? #include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; }

#include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; } for(x=0 ; x<5; x++) { printf(“\n The value of iArray index %d is %d \n”, x, iArray[x]); }

 How do you search through an array?

#include main() { int x; int iValue; int iFound = -1; int iArray[5]; for( x=0; x < 5 ; x++) iArray[x] = (x+x); printf(“\n Enter value to search for:”); scanf(“%d”, &iValue); for(x=0 ; x<5; x++) { if( iArray[x] ==iValue){ iFound =x; break; ) } if(iFound >-1) printf(“\n I found your search value in element %d \n”, iFound); else printf(“\n Sorry, your search value was not found \n”); }