Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 #include  Using a directive to include a header file  Stdio.h = standard input output header file.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Input & Output: Console
 data_type array_name [number-of-elements];  Two Dimensional Array array_type array_name [number_of_ROWS][number_of_COLUMNS];
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
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.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
1 C++ Syntax and Semantics, and the Program Development Process.
C Programming – Part 6 File Input and Output
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.
Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
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;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
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);
Please enter how long your name is: 21 Please enter your name: Nawaf Hello Nawaf Please enter how long your name is: -7 Failed allocation memory.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
 How a computer stores data in its internal memory  RAM (Random-Access Memory) - temporary  ROM (Read-Only Memory) – non volatile  Store data in bytes.
CECS 121 Final Test.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data.
Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
A Sample Program #include using namespace std; int main(void) { cout
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Computing with C# and the .NET Framework
Basic Elements of C++ Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 4: Control Structures I (Selection)
CECS 130 Midterm Exam Review Spring 2019
COMS 261 Computer Science I
DATA TYPES There are four basic data types associated with variables:
CECS 130 Final Exam Review Session
Files Chapter 8.
Presentation transcript:

Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen()  fclose()  fscanf()  fprintf()

 fopen() returns a FILE pointer back to the pRead variable #include Main() { FILE *pRead; pRead = fopen(“file1.dat”, “r”); if(pRead == NULL) printf(“\nFile cannot be opened\n”); else printf(“\nFile opened for reading\n”); }

 Pretty basic.

 Reads a single field from a data file  “%s” will read a series of characters until a white space is found  can do fscanf(pRead, “%s%s”, name, hobby);

#include Main() { FILE *pRead; char name[10]; pRead = fopen(“names.dat”, “r”); if( pRead == NULL ) printf( “\nFile cannot be opened\n”); else printf(“\nContents of names.dat\n”); fscanf( pRead, “%s”, name ); while( !feof(pRead) ) { printf( “%s\n”, name ); fscanf( pRead, “%s”, name ); }

Kelly11/12/866Louisville Allen04/05/7749Atlanta Chelsea03/30/9012Charleston Can you write a program that prints out the contents of this information.dat file?

#include Main() { FILE *pRead; char name[10]; char birthdate[9]; float number; char hometown[20]; pRead = fopen(“information.dat”, “r”); if( pRead == NULL ) printf( “\nFile cannot be opened\n”); else fscanf( pRead, “%s%s%f”, name, birthdate, &number, hometown ); while( !feof(pRead) ) { printf( “%s \t %s \t %f \t %s\n”, name, birthdate, &number, hometown ); fscanf( pRead, “%s%s%f”, name, birthdate, &number, hometown ); }

 The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes. printf()

#include Main() { FILE *pWrite; char fName[20]; char lName [20]; float gpa; pWrite = fopen(“students.dat”,”w”); if( pWrite == NULL ) printf(“\nFile not opened\n”); else printf(“\nEnter first name, last name, and GPA separated” printf(“Enter data separated by spaces:”); scanf(“%s%s%f”, fName, lName, &gpa); fprintf(pWrite, “%s \t %s \t %.2f \n”, fName, lName, gpa); fclose(pWrite); }

 Can you write a program that asks the user for their  Name  Phone Number  Bank account balance And then prints this information to a data file called accounts.dat ?

 Summary  Include #include directive at beginning of program  Use cin to take data from user  Use cout to display data on screen ▪ Display multiple strings and integers in the same cout statement by separating items with <<

#include using namespace std; string name = “”; int main(void) { cout<<“What is your name?”; cin>>name; cout<<endl<<“Hello”<<name.c_str(); return 0; }

#include using namespace std; int x = 25; string str2 = “This is a test”; int main( void ) { cout<<“Test”<<1<<2<<“3”; cout<<25 %7<<endl<<str2.c_str(); return 0; }

Test 1234 This is a test

 How a computer stores data in its internal memory  RAM (Random-Access Memory) - temporary  ROM (Read-Only Memory) – non volatile  Store data in bytes  How you store data temporarily  Create variables based on fundamental types (bool, char, int, float)  constants: #define CONSTNAME value  sizeof()

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)

 What do each of the following evaluate to? 1. long elves = 8; int dwarves = 8; if(elves==dwarves) //true or false? if(elves!=0) //true or false? 2. int elves = 4; int dwarves = 5; if(dwarves > (2/3)) //true or false? 3. if(0 < x < 99) //true or false? 4. if(0<= (0<1))//true or false?

 What do each of the following evaluate to? 1. long elves = 8; int dwarves = 8; if(elves==dwarves) //true if(elves!=0) //true 2. int elves = 4; int dwarves = 5; if(dwarves > (2/3)) //true 3. if(0 < x < 99) //true …TRUE (1) and FALSE (0) < if(0<= (0<1))//true

 if(condition) statement; else if (condition) statement;  condition ? expr1 : expr2  ex. z = ( x > y ) ? y : x ;  cannot do (x>y) ? count << “x is greater than y.” : cout << “x isn’t greater than y.”

switch(expression){ case expr1: statement; break; case expr2: statement; break; case expr3: statement; break; default: statements break; }

 while (condition) { statements; }  do { statements; } while(condition);

 for (initialization; condition; expression) { statements; }  Incrementing: Prefix and Postfix int x = 5; int y = 6; int z = y++ //z=6, y=7 postfix operator int z = ++x //z=6, x=6 prefix operator

KeywordPurpose breakExits the nearest enclosing “switch” statement or iteration statement continueStarts the next loop of the nearest enclosing iteration statement gotoJumps to a particular place in your code returnEnds a function and returns a value

 Can you write a program that prints out the following?

for ( int count = 0; count < 10; count ++) { cout <<count<<“”; }

1. Write a conditional statement that will assign x/y to x if y doesn’t equal Write a while loop that calculates the summative of positive integers from 1 to some number n. 3. Write a conditional statement that assigns x*y if x is even; otherwise, if x is odd and y doesn’t equal 0, assign x to x/y; if neither of the preceding cases is true, output to the screen that y is equal to 0.

 Function declaration  Function definition  Function call

#include using namespace std; int add(int a, int b); int main(void) { int number1, number2; cout << “Enter the first value to be summed:”’ cin >> number1; cout << “\nEnter the second:”; cin >> number2; cout << “\n The sum is: “ << add (number1, number2) <<endl; } int add(int a, int b) { return a+b; }

 Write a function, called multiply that multiplies two numbers and returns the result

 Declare classes  Create objects  3 MAIN PRINCIPLES OF OOP  Data abstraction – hiding data members and implementation of a class behind an interface so that the user of the class corrupt that data  Encapsulation – each class represents a specific thing or concept. Multiple classes combine to produce the whole  Polymorphism-objects can be used in more than one program

 Classes are general models from which you can create objects  Classes have data members either data types or methods  Classes should contain a constructor method and a destructor method  See handout for example of a program that utilizes a class

class ClassName { memberList }; memberList can be either data member declarations or method declarations

Class Bow { //data member declarations string color; bool drawn; int numOfArrows; Bow(string aColor); //constructor ~Bow(); //destructor //methods void draw(); int fire(); };

Return_type ClassName::methodName(argumentList) { methodImplementation }

//draws the bow Void Bow::draw() { drawn = true; cout<< “The “<<color<<“bow has been drawn.”<<endl; }

 Arrays  Pointers

 data_type array_name [number-of-elements];  Two Dimensional Array array_type array_name [number_ofelements1][number_of_elements2];

 type* pointer_name;  ex. int my_int; int* my_int_pointer = &my_int; Assigns the address of my_int to the pointer

 Copying strings from one to another  char* strcpy(char* p, const char* q);  char s[6]; strcpy(s, “Hello”);  To combine strings  char* strcat(char* p, const char* q);  char s[12] = “Hello” strcat(s, “World”);

 To copy n characters from q to the of p.  char* strncpy(char* p, const char* q, int n);  char s [7] = “Say “; char t[] = “Hi”; strncpy (s, t, 2)

 Can you write a program using C++ that uses a FOR loop to initialize a 2D array that looks like the following {0,5,10,15}{0,2,4,6}

#include using namespace std; int main(void) { int array[4][4]; for( int i = 0; i< 5; i++) {

 Basic framework for a program  How to Comment  How to Print  How to store variables  How to Print stored variables  How to find the size of a variable  How to convert from one data type to another  How to Declare Constants

 If statements  Conventional  Using conditional operator  Switch-case statements  Loops  While  Do-While  For  Branching statements

 How to declare and implement functions  How to create arrays  How to create pointers  Useful string functions  Classes

 //this is how you comment  /*this is how you comment */ Use for Multiple lines

 Used to create functions, classes, and variables of the same name  Ex. Namespace combat { void fire() } Namespace exploration { void fire() }

 To call a namespace combat::fire()  Say (to avoid having to put combat:: every time using namespace combat; fire()

class aClass { public: int anInt; } class aDerivedClass : public aClass { protected: float aFloat; };

 Used in place of a specific data type. For example, use a template to add data types together, whichever data type the user wishes (i.e integers, floats)