Ch 6-1. Structures. March 30, Prof. Young-Tak Kim
|
|
- Cory Lane
- 5 years ago
- Views:
Transcription
1 Ch 6-1. Structures March 30, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : ; Fax : ytkim@yu.ac.kr)
2 Outline Structures Structure types Structures as function arguments Initializing structures Passing structure as Parameter Returning structure from called function Structure Array ch
3 Structures 2 nd aggregate data type: struct Recall: aggregate meaning "grouping" Recall array: collection of values of same type Structure: collection of values of different types Treated as a single item, like arrays Major difference: Must first "define" struct Prior to declaring any variables ch
4 Structure Types Define struct globally (typically) No memory is allocated Just a "placeholder" for what our struct will "look like" Definition: typedef struct MyCmplx { // Name of new struct "type" double real; // member names double imagin; double mag; // magnitude } Cmplx; ch
5 Declare Structure Variable With structure type defined, now declare variables of this new type: Cmplx c1, c2; Just like declaring simple types Variable c1 and c2 are now of type Cmplx They contain "member values real, imagin, and mag Each of the struct "parts" ch
6 Accessing Structure Members Dot Operator to access members Cmplx c1; c1.real = 2.0; c1.imagin = 3.0; c1.mag = sqrt(pow(c1.real, 2.0) + pow(c1.imagin, 2.0) ); Called "member variables" The "parts (attribute) of the structure variable Different structs can have same name member variables No conflicts struct Student { int st_id; char name[20]; Date birthdate; }; struct Employ { int emp_id; char name[20]; Date birthdate; }; ch
7 typedef struct MyCmplx { double real; double imagin; double mag; } Cmplx; void printcmplx(cmplx c) { char sign; Structure Example } sign = (c.imagin < 0)? '-' : '+'; cout << setw(8) << c.real <<" " << sign << " j" << fabs(c.imagin) ; cout << " (magnitude " << c.mag << ")" << endl; Cmplx cmplxadd(cmplx c1, Cmplx c2) { Cmplx res; res.real = c1.real + c2.real; res.imagin = c1.imagin + c2.imagin; res.mag = sqrt(res.real*res.real + res.imagin*res.imagin); return res; } ch
8 Cmplx cmplxsub(cmplx c1, Cmplx c2) { Cmplx res; res.real = c1.real - c2.real; res.imagin = c1.imagin - c2.imagin; res.mag = sqrt(res.real*res.real + res.imagin*res.imagin); return res; } Cmplx cmplxmultiply(cmplx c1, Cmplx c2) { Cmplx res; res.real = (c1.real * c2.real) - (c1.imagin * c2.imagin); res.imagin = (c1.real * c2.imagin) + (c1.imagin * c2.real); res.mag = sqrt(res.real*res.real + res.imagin*res.imagin); return res; } Cmplx cmplxdivide(cmplx c1, Cmplx c2) { Cmplx res; double denom; denom = c2.real*c2.real + c2.imagin*c2.imagin; res.real = ((c1.real * c2.real) + (c1.imagin * c2.imagin)) / denom; res.imagin = ((c2.real * c1.imagin) - (c1.real * c2.imagin)) / denom; res.mag = sqrt(res.real*res.real + res.imagin*res.imagin); return res; } ch
9 1. int main() 2. { 3. Cmplx cmplxs[7]; 4. cout << "Input two complex numbers : c1.real, c1.imagin, 5. cout << c2.real, c2.imagin"<<endl; 6. cout << "c1.real : "; 7. cin >> cmplxs[0].real; 8. cout << "c1.imagin : "; 9. cin >> cmplxs[0].imagin; 10. cmplxs[0].mag = sqrt((cmplxs[0].real*cmplxs[0].real) (cmplxs[0].imagin * cmplxs[0].imagin)); 12. cout << "c2.real : "; 13. cin >> cmplxs[1].real; 14. cout << "c2.imagin : "; 15. cin >> cmplxs[1].imagin; 16. cmplxs[1].mag = sqrt((cmplxs[1].real*cmplxs[1].real) (cmplxs[1].imagin * cmplxs[1].imagin)); ch
10 18. cmplxs[2] = cmplxadd(cmplxs[0], cmplxs[1]); 19. cmplxs[3] = cmplxsub(cmplxs[0], cmplxs[1]); 20. cmplxs[4] = cmplxmultiply(cmplxs[0], cmplxs[1]); 21. cmplxs[5] = cmplxdivide(cmplxs[0], cmplxs[1]); 22. cmplxs[6] = cmplxs[0]; 23. for (int i=0; i<7; i++) { 24. cout << "cmplxs[" << i << "]: "; 25. printcmplx(cmplxs[i]); 26. } 27. return 0; 28.} ch
11 Structure Pitfall Semicolon after structure definition ; MUST exist: struct WeatherData { double temperature; double windvelocity; }; REQUIRED semicolon! Required since you "can" declare structure variables in this location ch
12 Structure Assignments Given structure named Cmplx Declare two structure variables: Cmplx c1, c2; Both are variables of "struct type Cmplx" Simple assignments are legal: c2 = c1; Simply copies each member variable from c1 into member variables of c2 ch
13 Structures as Function Arguments Passed like any simple data type Pass-by-value Pass-by-reference Or combination Can also be returned by function Return-type is structure type Return statement in function definition sends structure variable back to caller ch
14 Initializing Structures Can initialize at declaration Example: struct Planet { char name[10]; double relativemass; double distance; }; struct Planet earth = { Earth, 1.0, 150}; Declaration provides initial data to all three member variables ch
15 Array of Structure Struct data elements can be organized in array Example) struct Planet solarsystem[solar_planets] = { {"Mercury", , 57.9}, {"Venus", 0.815, 108}, {"Earth",1.0, 150}, {"Mars", 0.107, 228}, {"Jupiter", 318, 778}, {"Saturn", 95.1, 1430}, {"Uranus", 14.5, 2870}, {"Neptune", 17.2, 4500}, {"Pluto", 0.11, 5900} }; ch
16 Handling attributes of each element of struct array void printplanets(struct Planet solarplanets[], int num_planet) 1. { 2. cout.setf(ios::fixed); 3. cout.setf(ios::showpoint); 4. for (int i=0; i<num_planet; i++) { 5. cout << setw(2) << i; 6. cout << " Name: "; 7. cout << setiosflags(ios::left) <<setw(8) << solarplanets[i].name; 8. cout << resetiosflags(ios::left); 9. cout << " Rel Mass: "; 10. cout << setw(7) << setprecision(3) <<solarplanets[i].relativemass; 11. cout << " Dist from Sun: "; 12. cout << setw(6) << setprecision(1) << solarplanets[i].distance ; 13. cout << endl; 14. } // end for 15.} ch
17 Sorting Array of Structure Sorting elements in array of structure (1) void sortplanetsbyrelmass(struct Planet solarplanets[], int num_planet) 1. { 2. struct Planet temp; 3. int i, j, m; 4. double min_relmass; 5. for (i=0; i<num_planet-1; i++) { 6. m = i; 7. min_relmass = solarplanets[i].relativemass; 8. for (j=i+1; j<num_planet; j++) { 9. if (min_relmass > solarplanets[j].relativemass) { 10. m = j; 11. min_relmass = solarplanets[j].relativemass; 12. } 13. } // end inner for 14. if (m!= i) { // if new minimum found, swap 15. temp = solarplanets[i]; 16. solarplanets[i] = solarplanets[m]; 17. solarplanets[m] = temp; 18. } 19. } // end outer for 20. } ch
18 Sorting elements in array of structure (2) void sortplanetsbyname(struct Planet solarplanets[], int num_planet) { 1. struct Planet temp; 2. int i, j, m; 3. char min_name[10] = {0}; 4. for (i=0; i<num_planet-1; i++) { 5. m = i; 6. strcpy(min_name, solarplanets[i].name); 7. for (j=i+1; j<num_planet; j++) { 8. if (strcmp(min_name, solarplanets[j].name) > 0) { 9. m = j; 10. strcpy(min_name, solarplanets[j].name); 11. } 12. } // end inner for 13. if (m!= i) { // if new minimum found, swap 14. temp = solarplanets[i]; 15. solarplanets[i] = solarplanets[m]; 16. solarplanets[m] = temp; 17. } 18. } // end outer for 19.} ch
19 1. int main() { 2. struct Planet solarsystem[solar_planets] = 3. {{"Mercury", , 57.9}, {"Venus", 0.815, 108}, {"Earth",1.0, 150}, 4. {"Mars", 0.107, 228}, {"Jupiter", 318, 778}, {"Saturn", 95.1, 1430}, 5. {"Uranus", 14.5, 2870}, {"Neptune", 17.2, 4500}, {"Pluto", 0.11, 5900} }; 6. cout << "Initial state" << endl; 7. printplanets(solarsystem, SOLAR_PLANETS); 8. sortplanetsbyrelmass(solarsystem, SOLAR_PLANETS); 9. cout << " nafter sorting by relative mass:" << endl; 10. printplanets(solarsystem, SOLAR_PLANETS); 11. sortplanetsbydist(solarsystem, SOLAR_PLANETS); 12. cout << " nafter sorting by distance from sun:" << endl; 13. printplanets(solarsystem, SOLAR_PLANETS); 14. sortplanetsbyname(solarsystem, SOLAR_PLANETS); 15. cout << " nafter sorting by name using strcmp and strcpy:" << endl; 16. printplanets(solarsystem, SOLAR_PLANETS); 17. cout << " n n"; return 0; 20.} ch
20 result of execution ch
21 Homework Calendar system with structure for date 1) Design a structure for date that contains day, month, year, yearday and month_name. struct Date { int day; int month; int year; int yearday; char month_name[10]; }; yearday means the sequence number of the day from the January 1 of the year. 2) Input a date from console terminal in format of three integers for day, month, and year. For example, means April 12, Initialize the month_name of the input date, determine whether the year is leap year or not. And, calculate the yearday. In this date handling function, use dot. operator. ch
22 3) Write a function that increment the given date by the specified number of days. The prototype is void incrementdate(date& dptr, int days), which is using Call-by-Reference in parameter passing. Input an integer number, and increase the date initialized in step 2) (by the input integer data). The month and year must be calculated correctly at the date increment. 4) Write a function int datecompare(const Date& d1, const Date& d2) that compares two dates, and returns the difference of the two dates yearday. The function receives two arguments of Date structure, passed by call-by-pointer. The return type is integer. Using the first date (input from console) and the increased date, calculate the difference of the two dates using the datecompare() function. ch
Ch 6. Structures and Classes
2013-2 Ch 6. Structures and Classes September 1, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;
More informationCh 8. Operator Overloading, Friends, and References
2014-1 Ch 8. Operator Overloading, Friends, and References May 28, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel
More informationCh 5-2. Arrays Part 2
2014-1 Ch 5-2. Arrays Part 2 March 29, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742
More informationCS 115 Exam 3, Spring 2011
CS 115 Exam 3, Spring 2011 Your name: Rules You may use one handwritten 8.5 x 11 cheat sheet (front and back). This is the only resource you may consult during this exam. Explain/show work if you want
More informationChapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo
Chapter 6 Structures and Classes 1 Learning Objectives Structures Structure types Structures as function arguments Initializing structures Classes Defining, member functions Public and private members
More informationCh 4. Parameters and Function Overloading
2014-1 Ch 4. Parameters and Function Overloading March 19, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;
More informationCh 6-8. C-Strings. April 30, Prof. Young-Tak Kim
2014-1 Ch 6-8. C-Strings April 30, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742
More informationCh 6-2. File Input / Output
2014-1 Ch 6-2. File Input / Output March 30, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax
More informationCh 1. Algorithms and Basic C++ Programming
2013-2 Ch 1. Algorithms and Basic C++ Programming July 1, 2013 Dept. of Information & Communication Engineering College of Engineering Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742
More informationIndependent Representation
Independent Representation 1 Integer magnitude + 1 2 3 4 5 6 7 8 9 0 Float sign sign of exponent mantissa On standard 32 bit machines: INT_MAX = 2147483647 which gives 10 digits of precision, (i.e. the
More informationKingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS
Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationConsider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is " ". Given the function prototype: float test(int,
More information1 Pointer Concepts. 1.1 Pointer Examples
1 1 Pointer Concepts What are pointers? How are they used? Point to a memory location. Call by reference is based on pointers. Operators: & Address operator * Dereferencing operator Machine/compiler dependencies
More informationPIC 10A. Final Review: Part I
PIC 10A Final Review: Part I Final exam The final exam is worth 30% of your grade, same weight as 2 midterms. Could be 50% if grading option 2 turns out better for you. Length is also roughly 2 midterms
More informationCh 7. Constructors and Other Tools
2013-2 Ch 7. Constructors and Other Tools September 3, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationStructured Data. CIS 15 : Spring 2007
Structured Data CIS 15 : Spring 2007 Functionalia HW4 Part A due this SUNDAY April 1st: 11:59pm Reminder: I do NOT accept LATE HOMEWORK. Today: Dynamic Memory Allocation Allocating Arrays Returning Pointers
More informationHomework #3 CS2255 Fall 2012
Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation
More informationLab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1
Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More informationExam 3 Chapters 7 & 9
Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False
More informationReview of Important Topics in CS1600. Functions Arrays C-strings
Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why
More informationChapter 13. Strings. Introduction
Chapter 13 Strings 1 Introduction This chapter covers both string constants (or literals, as they re called in the C standard) and string variables. Strings are arrays of characters in which a special
More informationStrings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More informationCIS 190: C/C++ Programming. Classes in C++
CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationExercise 1.1 Hello world
Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example
More informationCMPS 221 Sample Final
Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str,
More informationC++ Programming Chapter 7 Pointers
C++ Programming Chapter 7 Pointers Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics & Department
More informationIV Unit Second Part STRUCTURES
STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure
More informationFor loops, nested loops and scopes. Jordi Cortadella Department of Computer Science
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science Outline For loops Scopes Nested loops Introduction to Programming Dept. CS, UPC 2 Calculate x y Algorithm: repeated multiplication
More informationCh 6-5. Bitwise Operations
2014-1 Ch 6-5. Bitwise Operations 2014. 4. 16. Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax :
More informationLAB 4.1 Relational Operators and the if Statement
LAB 4.1 Relational Operators and the if Statement // This program tests whether or not an initialized value of num2 // is equal to a value of num1 input by the user. int main( ) int num1, // num1 is not
More informationArrays. Week 4. Assylbek Jumagaliyev
Arrays Week 4 Assylbek Jumagaliyev a.jumagaliyev@iitu.kz Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays
More informationQuick review pointer basics (KR ch )
1 Plan for today Quick review pointer basics (KR ch5.1 5.5) Related questions in midterm Continue on pointers (KR 5.6 -- ) Array of pointers Command line arguments Dynamic memory allocation (malloc) 1
More informationOutline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x
Outline For loops, nested loops and scopes For loops Scopes Jordi Cortadella Department of Computer Science Nested loops Calculate x y Algorithm: repeated multiplication x x x x y times y x i p=x i 4 3
More informationFunction Call Example
Function Call Example A Function Call Example (1) ch 3-25 A Function Call Example (2) ch 3-26 Alternative Function Declaration Recall: Function declaration is "information for compiler Compiler only needs
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationLecture 23: Pointer Arithmetic
Lecture 23: Pointer Arithmetic Wai L. Khoo Department of Computer Science City College of New York November 29, 2011 Wai L. Khoo (CS@CCNY) Lecture 23 November 29, 2011 1 / 14 Pointer Arithmetic Pointer
More informationa data type is Types
Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental
More informationUEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter
UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with
More informationMemory and Pointers written by Cathy Saxton
Memory and Pointers written by Cathy Saxton Basic Memory Layout When a program is running, there are three main chunks of memory that it is using: A program code area where the program itself is loaded.
More informationOutline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006
Introduction to User-defined Functions Larry Caretto Computer Science 106 Computing in Engineering and Science March 21, 2006 Outline Why we use functions Writing and calling a function Header and body
More informationAryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9
Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1 Introduction 10.2 Structure Definitions 10.3 Initializing Structures 10.4 Accessing
More informationOBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2
More informationDataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries
2013-2 DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. November 22, 2013 Advanced
More informationBefore we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.
Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172 The lab will be on pointers In compensation, no class on Friday, Jan. 31. 1 Consider the bubble function one more
More informationComputer Programming
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,
More informationData Types. Chapter 8
Data Types Chapter 8 Chapter Topics Enumeration Data Types Declaration Assignment Operations Looping with Enumeration Types Anonymous Data Types The typedef statement Namespaces The string type 2 Enumeration
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationPointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am
Exam 1 on July 18, 2005 10:00-11:40am Pointers Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the
More informationLab 14 SERACHI G Dr. John P. Abraham
Lab 14 SERACHI G Dr. John P. Abraham We can only do a linear search on an unsorted array, but on a sorted array we can do a binary search. In this chapter we will study two different algorithms for searching,
More informationExam I Review Questions Fall 2010
Exam I Review Questions Fall 2010 The following review questions are similar to the kinds of questions you will be expected to answer on Exam I (scheduled for Oct. 14), which will cover LCR, chs. 1 7,
More informationCS 2530 INTERMEDIATE COMPUTING
CS 2530 INTERMEDIATE COMPUTING Spring 2016 1-8-2018 Michael J. Holmes University of Northern Iowa Object Oriented Concepts In some cases we ll learn the concepts prior to learning the terminology. Other
More informationtypedef int Array[10]; String name; Array ages;
Morteza Noferesti The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration: typedef int Length; Length a, b, len ; Length
More informationA structure is an aggregate data type which contains a fixed number of heterogeneous components.
Structures 1 Definition: A structure is an aggregate data type which contains a fixed number of heterogeneous components. Structure components are termed fields or members, each with a unique name. Each
More informationCharacter Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10
Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10 Ziad Matni Dept. of Computer Science, UCSB Lecture Outline Useful character manipulators & functions
More informationStructures. Lecture 15 COP 3014 Spring April 16, 2018
Structures Lecture 15 COP 3014 Spring 2018 April 16, 2018 Motivation We have plenty of simple types for storing single items like numbers, characters. But is this really enough for storing more complex
More informationGetting started with C++ (Part 2)
Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output
More informationScheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C
Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.
More information6.096 Introduction to C++
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. MASSACHUSETTS INSTITUTE
More informationArrays and functions Multidimensional arrays Sorting and algorithm efficiency
Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same
More informationWeek 3: Pointers (Part 2)
Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain
More informationSample Code: OUTPUT Daily Highs & Lows
Name1: Name2: Class Day / Time: Due Date: Sample Code: OUTPUT Daily Highs & Lows This program will obtain from the user 3 sets of data including a date, the high temperature and a low temperature for that
More informationComplex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL
Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL Many programs require complex data to be represented That cannot easily
More informationIntroduction to C Programming (Part C) Copyright 2008 W. W. Norton & Company. All rights Reserved
Introduction to C Programming (Part C) Copyright 2008 W. W. Norton & Company. All rights Reserved Overview (King Ch. 13, 22, 16-17) Strings (Ch. 13) Input/Output (Ch. 22) Structures (Ch. 16) Dynamic memory
More informationWhat is an algorithm?
Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address
More informationPage. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team
Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Question No: 1 ( Marks: 2 ) Write a declaration statement for an array of 10
More informationEng591 - Virtual Reality Programming First Mid-Term Exam
Eng591 - Virtual Reality Programming First Mid-erm Exam Fall 1999 You are allowed to use your coursepack only, ( including any notes written therein. ) No other assistance or references are permitted.
More informationFunctions, Arrays & Structs
Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions l Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier
More informationCS 1704 Homework 2 C++ Pointer Basics Fall 2002
Instructions: Opscan forms will be passed out in class on Tuesday, Sept 24. Write your name and code your ID number on the opscan form. Turn in your completed opscan at class on Thursday Sept 26, or at
More informationReference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that
Reference Parameters There are two ways to pass arguments to functions: pass-by-value and pass-by-reference. pass-by-value A copy of the argument s value is made and passed to the called function. Changes
More informationLearning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays
Chapter 5 Arrays Learning Objectives Introduction to Arrays Declaring and referencing arrays For-loops and arrays Arrays in memory Arrays in Functions Arrays as function arguments, return values Programming
More informationC++ PROGRAMMING SKILLS Part 4: Arrays
C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median
More informationLinked Lists CS 16: Solving Problems with Computers I Lecture #16
Linked Lists CS 16: Solving Problems with Computers I Lecture #16 Ziad Matni Dept. of Computer Science, UCSB Material: Everything we ve done Homework, Labs, Lectures, Textbook Tuesday, 12/12 in this classroom
More informationThe American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011
The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS
More informationPointers as Arguments
Introduction as Arguments How it Works called program on start of execution xw = &i xf = &d after excution xw = &i xf = &d caller program i? d? i 3 d.14159 x 3.14159 x 3.14159 R. K. Ghosh (IIT-Kanpur)
More informationPointers and Strings Prentice Hall, Inc. All rights reserved.
Pointers and Strings 1 Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using const with Pointers Selection Sort Using Pass-by-Reference 2
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationName SECTION: 12:45 2:20. True or False (12 Points)
Name SECION: 12:45 2:20 rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables
More informationmith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut
mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut dthiebaut@smithedu Outline A Few Words about HW 8 Finish the Input Port Lab! Revisiting Homework
More informationStrings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb
CS 201 String Debzani Deb Strings(2) Two interpretations of String Arrays whose elements are characters. Pointer pointing to characters. Strings are always terminated with a NULL characters( \0 ). C needs
More informationBasic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces
Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program
More informationFunctions, Arrays & Structs
Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationcalling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!
Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns
More informationChapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)
Chapter - Arrays 1.1 Introduction 2.1 Introduction.2 Arrays.3 Declaring Arrays. Examples Using Arrays.5 Passing Arrays to Functions.6 Sorting Arrays. Case Study: Computing Mean, Median and Mode Using Arrays.8
More informationPointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017
Pointer Arithmetic Lecture 4 Chapter 10 Robb T. Koether Hampden-Sydney College Wed, Jan 25, 2017 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 1 / 36 1 Pointer Arithmetic
More informationProgramming in C++: Assignment Week 2
Programming in C++: Assignment Week 2 Total Marks : 20 Each question carries one mark Right hand side of each question shows its Type (MCQ/MSQ) March 3, 2017 Question 1 Look at the code snippet below:
More informationLab Instructor : Jean Lai
Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.
More informationChapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types
Chapter 4 - Arrays 1 4.1 Introduction 4.2 Arrays 4.3 Declaring Arrays 4.4 Examples Using Arrays 4.5 Passing Arrays to Functions 4.6 Sorting Arrays 4.7 Case Study: Computing Mean, Median and Mode Using
More informationArrays 2 CS 16: Solving Problems with Computers I Lecture #12
Arrays 2 CS 16: Solving Problems with Computers I Lecture #12 Ziad Matni Dept. of Computer Science, UCSB Material: Post- Midterm #1 Lecture 7 thru 12 Homework, Labs, Lectures, Textbook Tuesday, 11/14 in
More informationcout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];
1 C++ Dynamic Allocation Memory needs were determined before program execution by defining the variables needed. Sometime memory needs of a program can only be determined during runtime, or the memory
More informationpointers + memory double x; string a; int x; main overhead int y; main overhead
pointers + memory computer have memory to store data. every program gets a piece of it to use as we create and use more variables, more space is allocated to a program memory int x; double x; string a;
More informationIntroduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More informationAlgorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14
Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14 Ziad Matni Dept. of Computer Science, UCSB Administra:ve Turn in Homework #12 Homework #13 is due Tuesday Lab
More informationCPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)
Name rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables retain their value
More informationTutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays
Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays Outline 13.1 Test-Driving the Salary Survey Application 13.2 Introducing Arrays 13.3 Declaring and Initializing Arrays 13.4 Constructing
More information