Jeff West - Quiz Section 3

Slides:



Advertisements
Similar presentations
Strings.
Advertisements

CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
ITEC 320 C++ Examples.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Welcome to Mario Kart!... Er… EECS 183 Discussion!
C++ Lesson 1.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Repetitive Structures
Katherine Kampf / kkampf
Chapter Topics The Basics of a C++ Program Data Types
I/O Streams File I/O 2-D array review
Jeff West - Quiz Section 2
Jie(Jay) Wang Week1 Sept. 30
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Week of 12/12/16 Test Review.
Computing and Statistical Data Analysis Lecture 2
Bill Tucker Austin Community College COSC 1315
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Pointers Psst… over there.
Basic Elements of C++ Chapter 2.
Pointers Psst… over there.
Introduction to Functions
Basics (Variables, Assignments, I/O)
C Stuff CS 2308.
Today in CS161 Week #3 Learn about… Writing our First Program
Pointers, Dynamic Data, and Reference Types
TIPS: How to Be Successful
Pointers & Functions.
CSC 102 Chabli Boler.
Govt. Polytechnic,Dhangar
Functions Pass By Value Pass by Reference
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
Jeff West - Quiz Section 4
Jeff West - Quiz Section 8
Jeff West - Quiz Section 6
Objectives You should be able to describe: The while Statement
Programming Language C Language.
Fundamental Programming
C++ Programming Lecture 20 Strings
Pointers & Functions.
(Dreaded) Quiz 2 Next Monday.
Variables in C Topics Naming Variables Declaring Variables
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1D Fall18: week 2
Presentation transcript:

Jeff West - Quiz Section 3 CSE 143 Section AD Quiz Section 3 6/26/2001 Jeff West - Quiz Section 3

Administrative “Stuff” Please sign the sheet that is coming around if: - You are not yet registered for 143. - You are registered in a different sec. There will be a quiz this Thursday! Anything through tomorrow’s lecture is fair game! 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 Getting Help I want to help you guys as much as I can, but here are a few pointers on how to get as much help as possible: Feel free to use any TA’s office hours. Use the newsgroup! If you come to my office hour, it would be very helpful if you could bring an idea of how your program will be structured with you – napkin scratchings are better than nothing  If your program is almost done with small errors, it will be most effective if you bring a copy of your source files with you to my office. 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 Homework 0 Issues Specification asks for an infinite loop! Make it clear that you intended for the loop to be infinite, either by “while(true)” or by commenting that it is an infinite loop. Comment -- especially at the top of each file and after each variable is declared! Use C++ strings as much as possible! Use bool types where it is natural – example, while(true) is more natural than while(1)! Use meaningful names for variables – example, double fahrenheit, celsius not double x, y! 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 Commenting At the top of each file you should include your name, id number, section, and instructor… for example: // main.cpp – CSE assignment 1, Summer 2001 // Y. Name, id #9999999, section AD, B. Tjaden In long functions variables should have meaningful names! For example, use fahrenheit and celsius, not x and y! The best resource for style guidelines is the style guidelines section of the webpage! You will be graded on style, so do pay attention to it! 6/26/2001 Jeff West - Quiz Section 3

C Strings vs. C++ Strings C++ strings operate in a very natural manner when you are used to other datatypes (ints, doubles, etc.) string color1, color2; color1 = “red”; color2 = color1; if(color1 == color2) { cout << “This is true”; } 6/26/2001 Jeff West - Quiz Section 3

C Strings May Still Come Up… In this week’s homework assignment, it was suggested that you may wish to use the “strtok” function. This function specifically brings in a C-String to break apart and a C-String of dilimiters, and returns a C-String! This function will not let you send in C++ strings! 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 strtok Learn to use the Help section on the toolbar if you are using MSVC++! If you look up strtok, it says: char *strtok( char *strToken, const char *strDelimit ); On the handout you will see the example of using this function that the MSDN Library provides. 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 MSDN’s strtok Example #include <string.h> #include <stdio.h> char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; void main( void ) { printf( "%s\n\nTokens:\n", string ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) /* While there are tokens in "string" */ printf( " %s\n", token ); /* Get next token: */ token = strtok( NULL, seps ); } 6/26/2001 Jeff West - Quiz Section 3

Output According to MSDN A string of ,,tokens and some more tokens Tokens: A string of tokens and some more 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 Student Statistics! You are given last week’s student.h header file and told that the promptStudent function correctly prompts a student for their information and stores it in the student variable that is passed in. Make a program which will prompt student’s for their information until the height input is 0.0. The student who’s height is 0.0 does NOT count as a valid student. At this point, print to the screen: “There are w males and x females in the class.” “The oldest student in the class is y at age z.” You do not have to account for multiple students of the same age. 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 student.h // CSE143 Su01 Quiz Section 3 Example // student.h - interface to student data structure and functions // JW 6/01 #include <string> #include <iostream> using namespace std; enum Gender { MALE, FEMALE, NA }; // creates a new datatype Gender struct Student { // description of a single student: string name; // name Gender gender; // gender int age; // age double height; // height }; // Prompt a student for their information void promptStudent(Student &aStudent); More prototypes? Maybe, // Initializes a student so that its information is obviously void! void initializeStudent(Student &aStudent); 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 main.cpp (1) // CSE143 Su01 Quiz Section 3 Example // main.cpp - main program // JW 6/01 #include "student.h" // main program int main() { Student currentStudent; // the student that is inputting // their information Student oldestStudent; // oldest student in the class int males = 0, // the numbers of males and females = 0; // females in the class // initialize oldest student initializeStudent(oldestStudent); promptStudent(currentStudent); 6/26/2001 Jeff West - Quiz Section 3

Jeff West - Quiz Section 3 main.cpp (2) while(currentStudent.height != 0.0) { // account for student's gender if(currentStudent.gender == MALE) males++; else if(currentStudent.gender == FEMALE) females++; // if student is the oldest student, set variables accordingly if(currentStudent.age > oldestStudent.age) oldestStudent = currentStudent; promptStudent(currentStudent); } cout << "There are " << males << " males and " << females << " females in the class." << endl; cout << "The oldest student in the class is " << oldestStudent.name << " at age " << oldestStudent.age << '.' << endl; return 0; 6/26/2001 Jeff West - Quiz Section 3

student.cpp -- initializeStudent // Initializes a student so that its information is obviously void! void initializeStudent(Student &aStudent) { aStudent.name = "Nobody"; aStudent.gender = NA; aStudent.age = 0; aStudent.height = 0.0; } 6/26/2001 Jeff West - Quiz Section 3