实验11.28.

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Standard Algorithms. Many algorithms appear over and over again, in program after program. These are called standard algorithms You are required to know.
True or false A variable of type char can hold the value 301. ( F )
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
Chapter 5: Loops and Files.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Computer Science 1620 Programming & Problem Solving.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Java vs. You.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
1 Lecture 3 Part 1 Functions with math and randomness.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
First tutorial.
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
Functions Why we use functions C library functions Creating our own functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Overview of Programming and Problem Solving Textbook Chapter 1 1.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Loops and Files. 5.1 The Increment and Decrement Operators.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
CS320n – Elements of Visual Programming Assignment Help Session.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
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.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
CSC111 Quick Revision.
while Repetition Structure
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
Chapter 2 Assignment and Interactive Input
Chapter 4 Loops DDC 2133 Programming II.
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Presentation transcript:

实验11.28

Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die, and should call rand again to roll the second die. The sum of the two values should then be calculated. Roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each sum appears. Print the results in a tabular format.

Modify the program to use a two-dimensional array similar to the diagram in Figure L 7.1. Now, rather than counting the number of times each sum appears, increment the correct cell in the array. Print this array with the number of times each dice combination occurred. A sample output may look like the following:

Lab6_Dubugging The following program in this section does not run properly. Fix all the compilation errors so that the program will compile successfully. Once the program compiles, compare the output with the sample output, and eliminate any logic errors that may exist. The sample output demonstrates what the program’s output should be once the program’s code has been corrected.

Lab6_hom1 1. Use a one-dimensional array to solve the following problem. A company pays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $5000, or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount): a) $200–299 b) $300–399 c) $400–499 d) $500–599 e) $600–699 f) $700–799 g) $800–899 h) $900–999 i) $1000 and over

2. Use a one-dimensional array to solve the following problem: Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it is not a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.

Lab7_hom1(7.32) 递归/回文 例如:rahar

bool testPalindrome( const char [], int, int ); int main(){ const int SIZE = 80; // maximum size for string char c; // used to temporarily hold keyboard input char string[ SIZE ],copy[ SIZE ]; int count = 0; int copyCount; int i; // used for loop control in string copying cout << "Enter a sentence:\n"; while ( ( c = cin.get() ) != '\n' && count < SIZE ) string[ count++ ] = c; string[ count ] = '\0'; // terminate cstring for ( copyCount = 0, i = 0; string[ i ] != '\0'; i++ ){ if ( string[ i ] != ' ' ) copy[ copyCount++ ] = string[ i ]; } // end for // Print whether or not sentence is palindrome if ( testPalindrome( copy, 0, copyCount - 1 ) ) cout << '\"' << string << "\" is a palindrome" << endl; else cout << '\"' << string << "\" is not a palindrome" << endl; return 0; // indicates successful termination } // end main

// function to see if sentence is a palindrome bool testPalindrome( const char array[], int left, int right ) { if ( left == right || left > right ) return true; else if ( array[ left ] != array[ right ] ) return false; else return testPalindrome( array, left + 1, right - 1 ); } // end function testPalindrome

Lab7_home2(7.37) 递归、查找数组中的最小值

const int MAXRANGE = 1000; int recursiveMinimum( const int [], int, int ); int main() { const int SIZE = 10; int array[ SIZE ], int smallest; srand( time( 0 ) ); // initialize elements of array to random numbers for ( int loop = 0; loop < SIZE; loop++ ) array[ loop ] = 1 + rand() % MAXRANGE; cout << "Array members are:\n"; // display array for ( int k = 0; k < SIZE; k++ ) cout << setw( 5 ) << array[ k ]; cout << '\n'; smallest = recursiveMinimum( array, 0, SIZE - 1 ); cout << "\nSmallest element is: " << smallest << endl; return 0; // indicates successful termination } // end main

// function to recursively find minimum array element int recursiveMinimum( const int array[], int low, int high ) { static int smallest = MAXRANGE; // if first element of array is smallest so far // set smallest equal to that element if ( array[ low ] < smallest ) smallest = array[ low ]; // if only one element in array, return smallest // else recursively call recursiveMinimum with new subarray return low == high ? smallest : recursiveMinimum( array, low + 1, high ); } // end function recursiveMinimum