Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Similar documents
BITG 1113: Array (Part 1) LECTURE 8

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

CMPS 221 Sample Final

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

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

Introduction to Algorithms and Programming (COMP151)

LAB 4.1 Relational Operators and the if Statement

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

CSci 1113 Midterm 1. Name: Student ID:

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

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

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

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Arrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]

Computer Programming

FORM 2 (Please put your name and form # on the scantron!!!!)

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Arrays in C++ Instructor: Andy Abreu

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

CS 101, Spring 2016 March 22nd Exam 2

Exam 3 Chapters 7 & 9

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

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

TWO DIMENSIONAL ARRAY OR MATRIX common name scores Defined as scores[10][5] IE 10 rows x 5 columns showing name of each position; some values below

Practice Exercises #8

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

C++ Arrays. C++ Spring 2000 Arrays 1

EP241 Computing Programming

Declaring a 2D Array

1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code

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

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

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

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

Write a program that displays all the even integers between 1 and 100, inclusive

Computer Programming

For example, let s say we define an array of char of size six:

CS 115 Midterm 2A Solutions

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

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

Downloaded from

C++ PROGRAMMING SKILLS Part 4: Arrays

Data Structures CSci 1200 Test 2 Questions

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

Computer Science & Engineering 150A Problem Solving Using Computers

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

EP578 Computing for Physicists

Loops / Repetition Statements

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

1 Unit 8 'for' Loops

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

tablica: array: dane_liczbowe

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013

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

Add Subtract Multiply Divide

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Introduction to Programming I COS1511 School of Computing Revision Notes

2. ARRAYS What is an Array? index number subscrip 2. Write array declarations for the following: 3. What is array initialization?

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CSCE 2004 Midterm Exam Spring 2017

CS 115 Exam 2, Fall 2009

University of Dublin

CS222 Homework 4(Solution)

Matrices. Jordi Cortadella Department of Computer Science

CSci 1113 Midterm 2. Name: Student ID:

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Computer Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

True or False (14 Points)

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays

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)

Columns A[0] A[0][0] = 20 A[0][1] = 30

CS 201 (Intro. to Computing) Fall 2015 Sabancı University Sample Questions for Midterm 1

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Lab Instructor : Jean Lai

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.

Computer Programming

In this chapter you will learn:

Chapter 7 Solved problems

Computer Programming

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

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

CHAPTER 3 Expressions, Functions, Output

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Programming Language. Functions. Eng. Anis Nazer First Semester

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Transcription:

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

One Dimensional Q1: Write a program that declares two arrays of integers and fills them from the user. Then exchanges their values and display the resulted two arrays. int arr1[10], arr2[10]; cout << "Enter the first array values\n"; cin >> arr1[i]; cout << "Enter the second array values\n"; cin >> arr2[i]; int temp; temp = arr1[i]; arr1[i] = arr2[i]; arr2[i] = temp; cout << "The first array values now are:\n"; cout << arr1[i] << " "; cout << "The second array values now are:\n"; cout << arr2[i] << " "; P a g e 2

Q2: Write a program that takes 10 integers as input and prints the largest integer and its location in the array. int arr[10]; cout << "Enter the array values\n"; cin >> arr[i]; int max = arr[0], loc = 0, i; for (i=1; i<10; i++) if (arr[i] > max) max = arr[i]; loc = i; cout << "The maximum value is: " << max << " and it is in location " << loc << endl; P a g e 3

Q3: Write a program that defines a one dimensional array of integers of size 10 and fill it from the user. Then find the negative values and store them in another array and display new array. int arr[10]; cout << "Enter the array values\n"; cin >> arr[i]; int new_arr[10], index = 0; if (arr[i] < 0) new_arr[index++] = arr[i]; cout << "The new array values are:\n"; for (int i=0; i<index; i++) cout << new_arr[i] << " "; P a g e 4

Q4: Write a program that defines a one dimensional array of integers of size 11 and fill the first 10 values from the user. Then calculate the summation of the 10 values and store it in the 11 th location. int arr[11]; arr[10] = 0; cout << "Enter the array values\n"; cin >> arr[i]; arr[10] += arr[i]; cout << "The new array values are:\n"; for (int i=0; i<11; i++) cout << arr[i] << " "; P a g e 5

Q5: Write a C++ program that reverses the order of a one-dimensional array of size 10 without using another array. int x, j=9, a[10] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; for(int i=0; i<(10/2); i++) x = a[i]; a[i] = a[j]; a[j] = x; j--; for(int i=0; i<10; i++) cout << a[i] << " "; P a g e 6

Q6: Write a C++ program that declares two arrays of size 5. Read the first array from the user and store the factorial of each element in this array in its corresponding location in the second array. double a[5], b[5]; int fact; cout << "Enter the first array values\n"; for(int i=0; i<5; i++) cin >> a[i]; fact = 1; for(int j=1; j<=a[i]; j++) fact *= j; b[i] = fact; cout << "The factorials array values are:\n"; for (int i=0; i<5; i++) cout << b[i] << " "; P a g e 7

Two Dimensional Q7: Write a program that adds up two [4x4] arrays and stores the sum in a third array. int a[4][4], b[4][4]; cout << "Enter the first array values\n"; cin >> a[i][j]; cout << "Enter the second array values\n"; cin >> b[i][j]; int c[4][4]; c[i][j] = a[i][j] + b[i][j]; cout << "The factorials array values are:\n"; cout << c[i][j] << " "; P a g e 8

Q8: Write a C++ program that declares a tow dimensional array of size [4x4] and generates the values of its elements using conditional statements as the following: The main diagonal contains 0 in all its locations The upper triangle contains 1 in all its locations The lower triangle contains 2 in all its locations int a[4][4]; if(i<j) a[i][j] = 1; else if (i==j) a[i][j] = 0; else a[i][j] = 2; cout << "The factorials array values are:\n"; cout << a[i][j] << " "; P a g e 9

Q9: Write a program that defines a two-dimensional array of integers of size 4*4. The program will fill the array of values using the equation array_name[i][j] = i+j+2 (i refers to the row index, j refers to the column index). Then, define a one-dimensional array of size 4. The one-dimensional array should be filled with the values along the main diagonal of the two-dimensional array. For example: If the two-dimensional array is: V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 The one-dimensional array will be: V1 V6 V11 V16 #include <iostream> int a[4][4]; a[i][j] = i+j+2; int b[4], index=0; if(i==j) b[index] = a[i][j]; index++; P a g e 10

Q10: Write a program that defines a two-dimensional array of integers of size 10*10. The program will fill each location of the array of it index summation (array[i][j] = i+j). Then print the summation of the elements on the array circumference as shown below: #include <iostream> int a[4][4]; a[i][j] = i+j; int sum = 0; if(i==0 j==0 i==9 j==9) sum += a[i][j]; cout << "The result is: " << sum << endl; P a g e 11

Q11: Write a program that stores the grades of 5 students in a two-dimensional array. Each student has 3 marks. Each row will represent a student and the last cell in the row will store the calculated average for the student's marks. Finally, display the average of all student averages. #include <iostream> float a[5][4], sum; for(int i=0; i<5; i++) sum = 0; cout << "Enter the 3 marks for the student number: " << i+1 << endl; for(int j=0; j<3; j++) cin >> a[i][j]; sum += a[i][j]; a[i][3] = sum/3; cout << "The students marks and averages are:" << endl; for(int i=0; i<5; i++) cout << a[i][j] << " "; sum = 0; for(int i=0; i<5; i++) sum += a[i][3]; cout << "The AVG for all sudents is: " << (float)sum/5 << endl; P a g e 12