Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Similar documents
LECTURE 17. Array Searching and Sorting

Chapter 10 - Notes Applications of Arrays

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

1 Short Answer (5 Points Each)

Lab 8 The Stack (LIFO) Structure

Programming Language. Functions. Eng. Anis Nazer First Semester

Page 1 Name: CUNYfirstID: CS111 Summer Term 2 - Final 6/26/18

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Lab Instructor : Jean Lai

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Chapter Four: Loops II

Perfect square. #include<iostream> using namespace std; int main(){ int a=1; int square; while(true){ } cout<<square<<endl; }

Lab 7 (50 pts) Due Sunday, May 20)

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Review Questions for Final Exam

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

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.

CSCI 111 Second Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, November 29, 2017

Problem Solving: Storyboards for User Interaction

Lecture 23: Pointer Arithmetic

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Linked List using a Sentinel

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Page 1 Name: CUNYfirstID: CS111 Summer Term 1- Final 6/27/18

Quadratic Sorting Algorithms

CS 115 Midterm 2A Solutions

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Arrays. Week 4. Assylbek Jumagaliyev

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

CMPS 221 Sample Final

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Exercise 1.1 Hello world

BITG 1113: Array (Part 1) LECTURE 8

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

pointers + memory double x; string a; int x; main overhead int y; main overhead

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

CSCI 111 Second Midterm Exam Spring Solutions 09.05am 09.55am, Monday, May 07, 2018

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Review Questions for Final Exam KEY

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Ch 6. Functions. Example: function calls function

CS 115 Exam 3, Fall 2009

Rubik's Cube Algorithm

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

#include <iostream> using namespace std;

Study Guide for Test 2

CMSC 202 Midterm Exam 1 Fall 2015

EECE.3220: Data Structures Spring 2017

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

Due Date: See Blackboard

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Midterm Practice Exam

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, November 24, 2015

Programming Abstractions

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CSCE 110 PROGRAMMING FUNDAMENTALS

Review Questions II KEY

Due Date: See Blackboard

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions.

Increment and the While. Class 15

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Programming in C++: Assignment Week 8

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Object oriented programming

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

do { statements } while (condition);

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Bruce Merry. IOI Training Dec 2013

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Data Structures CSci 1200 Test 2 Questions

Due Date: See Blackboard

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS302 - Data Structures using C++

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

C++ For Science and Engineering Lecture 12

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Exceptions, Case Study-Exception handling in C++.

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

moretosearch = (location < length);

Getting started with C++ (Part 2)

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Transcription:

r6.14 For the operations on partially filled arrays below, provide the header of a func tion. d. Remove all elements that are less than a given value. Sol a. void remove_items_less_than(int arr[], int size, int value) P6.1 Write a program that initializes an array with ten random integers and then prints four lines of output, containing Every element at an even index. Every even element. All elements in reverse order. Only the first and last element. Sol #include <iostream> #include <ctime> using namespace std; int main() int randoms[10]; srand(time(0)); for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); cout << "Elements at even indexes: "; for (int j = 0; j < 10; j = j + 2) cout << randoms[j] << " "; cout << endl << "Even elements: "; for (int k = 0; k < 10; k++) if (randoms[k] % 2 == 0) cout << randoms[k] << " "; cout << endl << "All elements in reverse: "; for (int m = 9; m >= 0; m--) 1

cout << randoms[m] << " "; cout << endl << "First and last elements: "; cout << randoms[0] << " " << randoms[9] << endl; return 0; P6.2 Write array functions that carry out the following tasks for an array of integers: For each function, provide a test program. a. Swap the first and last element in an array. Sol a. Swap first and last elements in an array. #include <iostream> #include <ctime> using namespace std; void swap_first_last(int arr[], int size) int last = size - 1; int temp = arr[0]; arr[0] = arr[last]; arr[last] = temp; int main() int randoms[10]; srand(time(0)); 2

// Create a test array containing random numbers. for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); // Print the values as they are assigned. cout << randoms[i] << " "; // Perform the swap. swap_first_last(randoms, 10); // Print again to see new order. for (int i = 0; i < 10; i++) cout << randoms[i] << " "; return 0; b. Shift all elements by one to the right and move the last element into the first position. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16. b. Shift all elements by one to the right. 3

#include <iostream> #include <ctime> using namespace std; void rotate_right(int arr[], int size) int last = size - 1; int temp = arr[last]; for (int i = last; i > 0; i--) arr[i] = arr[i - 1]; arr[0] = temp; int main() int randoms[10]; srand(time(0)); // Create a test array containing random numbers. for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); 4

// Print the values as they are assigned. cout << randoms[i] << " "; // Rotate the array once to the right. rotate_right(randoms, 10); // Print again to see new order. for (int i = 0; i < 10; i++) cout << randoms[i] << " "; return 0; c. Replace all even elements with 0. c. Replace all even elements with 0. #include <iostream> #include <ctime> using namespace std; void replace_even(int arr[], int size) 5

for (int i = 0; i < size; i++) if (arr[i] % 2 == 0) // Number is even arr[i] = 0; int main() int randoms[10]; srand(time(0)); // Create a test array containing random numbers. for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); // Print the values as they are assigned. cout << randoms[i] << " "; // Replace the even elements. replace_even(randoms, 10); 6

// Print again to see new elements. for (int i = 0; i < 10; i++) cout << randoms[i] << " "; return 0; e. Remove the middle element if the array length is odd, or the middle two elements if the length is even. e. Remove the middle element if the array length is odd, or middle two if even. #include <iostream> #include <ctime> using namespace std; void remove_middle(int arr[], int &size) if (size % 2 == 0) // Size is even // Figure out starting point for removal int first_to_remove = size / 2-1; // Remove middle two elements for (int i = first_to_remove; i < size - 2; i++) 7

arr[i] = arr[i + 2]; size = size - 2; else // Size is odd // Figure out starting point for removal int first_to_remove = size / 2; // Remove middle element for (int i = first_to_remove; i < size - 1; i++) arr[i] = arr[i + 1]; size = size - 1; int main() int a_size = 11; int randoms[11]; srand(time(0)); // Create a test array containing random numbers. for (int i = 0; i < a_size; i++) 8

randoms[i] = (rand() % 100 + 1); // Print the values as they are assigned. cout << randoms[i] << " "; // Remove the middle element(s) remove_middle(randoms, a_size); // Print again to see new order. for (int i = 0; i < a_size; i++) cout << randoms[i] << " "; return 0; j. Return true if the array contains duplicate values (which need not be adjacent). j. Returns true if array contains duplicate values (not necessarily adjacent). #include <iostream> using namespace std; 9

bool contains_duplicates(int arr[], int size) // Assume no dupes. bool dupes = false; // Loop through array, checking for duplicates for (int i = 0; i < size; i++) for (int j = i + 1; j < size; j++) if (arr[i] == arr[j]) dupes = true; return dupes; int main() int arr_1[10] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 42; int arr_2[10] = 2, 1, 3, 4, 5, 4, 7, 4, 9, 4; // Check if array 1 has dupes. 10

if (contains_duplicates(arr_1, 10)) cout << "Array contains duplicates." << endl; else cout << "Array does NOT contain duplicates." << endl; // Check if array 2 dupes. if (contains_duplicates(arr_2, 10)) cout << "Array contains duplicates." << endl; else cout << "Array does NOT contain duplicates." << endl; return 0; P6.5 Write a function void remove_min that removes the minimum value from a partially filled array without calling other functions. #include <iostream> using namespace std; void remove_min(double input[], int &size) 11

// Find the minimum value first double min = input[0]; int min_pos = 0; for (int i = 1; i < size; i++) if (input[i] < min) min = input[i]; min_pos = i; // Now "eliminate" the minimum value for (int j = min_pos; j < size - 1; j++) input[j] = input[j + 1]; // Decrement the size size--; int main() const int SIZE = 9; double my_array[size] = 22.3, 22.1, 20.3, 17.7, 45.2, 12

11.9, 11.8, 21.6, 22.1; int my_size = SIZE; remove_min(my_array, my_size); for (int i = 0; i < my_size; i++) cout << my_array[i] << " "; return 0; P6.9 Write a function bool equals(int a[], int a_size, int b[], int b_size) that checks whether two arrays have the same elements in the same order. #include <iostream> using namespace std; bool equals(int a[], int a_size, int b[], int b_size) if (a_size!= b_size) 13

return false; for (int i = 0; i < a_size; i++) if (a[i]!= b[i]) return false; return true; int main() const int SIZE1 = 8; const int SIZE2 = 9; int arr1[size1] = 1, 2, 3, 4, 5, 6, 7, 8; int arr2[size1] = 1, 2, 3, 4, 5, 6, 7, 8; int arr3[size2] = 1, 2, 3, 4, 5, 6, 7, 8, 9; int arr4[size1] = 1, 2, 3, 4, 5, 4, 7, 8; cout << "Arrays 1 and 2 are "; if (!equals(arr1, SIZE1, arr2, SIZE1)) cout << "not "; 14

cout << "equal." << endl; cout << "Arrays 1 and 3 are "; if (!equals(arr1, SIZE1, arr3, SIZE2)) cout << "not "; cout << "equal." << endl; cout << "Arrays 1 and 4 are "; if (!equals(arr1, SIZE1, arr4, SIZE1)) cout << "not "; cout << "equal." << endl; return 0; Extra --- 2 Dimensional Arrays 1. This program calculates and prints a multiplication table for all values between 1 and 9 (inclusive). Note that when printing the table, the for loops start from 1 instead of 0. This is to omit printing the 0 column and 0 row. 15

2. 16

17