CS1073 Exam 3, Fall 2009 page 1. Important test instructions code fragments

Similar documents
Declaration and Initialization

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

CMSC 201 Fall 2016 Homework 6 Functions

Introduction to Fortran Programming. - input / output -

NO CALCULATOR ALLOWED!!

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Comparison of x with an entry in the array

ECE 563 Spring 2012 First Exam

CS 101 Spring 2007 Midterm 2 Name: ID:

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

Introduction to Computer Science Midterm 3 Fall, Points

BASIC ARRAYS IN FORTRAN

Assertions & Verification & Example Loop Invariants Example Exam Questions

XVIII Open Cup named after E.V. Pankratiev Stage 1: Grand Prix of Romania, Sunday, September 17, 2017

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

APCS Semester #1 Final Exam Practice Problems

Problem 1: Hello World!

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

Assertions & Verification Example Exam Questions

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Short Answer Questions (40 points)

Section A Arithmetic ( 5) Exercise A

Matrix Inverse 2 ( 2) 1 = 2 1 2

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

CS 433 Homework 4. Assigned on 10/17/2017 Due in class on 11/7/ Please write your name and NetID clearly on the first page.

cs1114 REVIEW of details test closed laptop period

Fortran 90 Two Commonly Used Statements

Review More Arrays Modules Final Review

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

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Array Processing { Part II. Multi-Dimensional Arrays. 1. What is a multi-dimensional array?

COMP 110 Practice Exercises for Midterm Solutions

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

7. Procedures and Structured Programming

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CHAPTER 1B: : Foundations for Algebra

Question: Total Points: Score:

Data Structures CHAPTER 11. Review Questions. Multiple-Choice Questions. Exercises. (Solutions to Odd-Numbered Problems)

CSc 520 Principles of Programming Languages

ROSE-HULMAN INSTITUTE OF TECHNOLOGY

Computers in Engineering. Subroutines Michael A. Hawker

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Computer Science & Engineering 150A Problem Solving Using Computers

CS1004: Intro to CS in Java, Spring 2005

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

CS Exam 2 Study Suggestions

Computer Organization EE 3755 Midterm Examination

Integers and Absolute Value. Unit 1 Lesson 5

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CS2255 HOMEWORK #1 Fall 2012

COP 3014 Honors: Spring 2017 Homework 5

Digital Design using HDLs EE 4755 Midterm Examination

CS 051 Homework Laboratory #2

Casting in C++ (intermediate level)

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Counting Loop: DO-END DO. DO var = initial-value, final-value, step-size

Computer Organization EE 3755 Midterm Examination

CS 101, Spring 2016 March 22nd Exam 2

CS302: Self Check Quiz 2

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

MACS 261J Final Exam. Question: Total Points: Score:

Elementary Statistics. Organizing Raw Data

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

File Input and Output

Computer Science Foundation Exam. March 3, Section I A. No Calculators! Name: SSN: In this section of the exam, there are three (3) problems.

Data Structures and Algorithms Key to Homework 1

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

COMPETENCY 1.0 UNDERSTAND THE STRUCTURE OF THE BASE TEN NUMERATION SYSTEM AND NUMBER THEORY

CS1100 Introduction to Programming

CS150 Sample Final Solution

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

***PRACTICE EXAM 02 for*** CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Actual exam scheduled for: 11/18/2009

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

QUADRATICS Geometric Applications of Quadratics Common Core Standard

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

Our Strategy for Learning Fortran 90

Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not.

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

2010Fa CS10 Online Final Answers Section 1

Allocating Storage for 1-Dimensional Arrays

Faculty of Applied Science and Engineering, University of Toronto CSC181: Introduction to Computer Programming, Fall 2000

Let us start with a quick revision of algebra and how to work with variables. Now let us look at the simpler topic, namely Substitution.

Module 16: Data Flow Analysis in Presence of Procedure Calls Lecture 32: Iteration. The Lecture Contains: Iteration Space.

Algorithms: The recipe for computation

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

CS 161 Exam II Winter 2018 FORM 1

Lecture Transcript While and Do While Statements in C++

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

All answers will be posted on web site, and most will be reviewed in class.

1-6 Order of Operations

REPETITIVE EXECUTION: LOOPS

Transcription:

CS1073 Exam 3, Fall 2009 page 1 Name (please print): Important test instructions code fragments Throughout this exam, you will be asked to write a code fragment based on certain assumptions (eg., assume there is a 2-D INTEGER array IA). Any time you are asked to write a code fragment, you should not attempt to write a full working program, but just a section of code that does the required operation. You will often need additional variable declarations (eg., you need a variable for use as a loop counter); in these cases show all declarations of new variables at the beginning of your code fragment (you should assume that IMPLICIT NONE is in effect). Only a few of the simpler questions at the beginning will ask for full working programs. Unless it is specified, you should assume that any arrays have been initilized in some legal fashion before reaching your code fragment. Questions that require a full program will ask you to write a program, while those that don t will ask you to write a code fragment. 1. Write a program that will print out every fourth number between 8 and 800 (inclusive) using a counter loop (6 pts) PROGRAM test IMPLICIT NONE INTEGER :: i DO i = 8, 800, 4 PRINT *, i END PROGRAM test pg pts: 6 tot pts: 6

CS1073 Exam 3, Fall 2009 page 2 2. Write a program that will print out every tenth number between 100 and 10,000 (inclusive) using a logical loop (if you don t remember which loop is which, be sure to write this program using a different style of loop than the one you used in the last problem). (6 pts) PROGRAM test IMPLICIT NONE INTEGER :: i i = 100 DO IF (i.gt. 10000) EXIT PRINT *, i i = i + 10 END PROGRAM test pg pts: 6 tot pts: 12

CS1073 Exam 3, Fall 2009 page 3 3. Write a program that will prompt the user for an integer N. Your program will then print out the square of the first N numbers starting from 1. For instance, if the user entered 5, your output should be: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 (remember that squaring a number consists of multiplying that number by itself, which is equivalent to the number to the power of 2). You may assume that the user will always enter a strictly positive number (zero or negative numbers will not be entered). (10 pts) PROGRAM test IMPLICIT NONE INTEGER :: i, N PRINT *, Enter the last number to square: READ *, N DO i = 1, N WRITE (*,1000) i, i*i 1000 format(i4, squared is, i14) pg pts: 10 tot pts: 22

CS1073 Exam 3, Fall 2009 page 4 4. Write a program that reads in a variable-length array from a file using loops (i.e. you cannot use array subsections). The first line of the file provides the length of the array, and each additional line of the file provides exactly one element of the array. The array is known to be of type REAL, but has no known maximum size. The input filename is array.in. (12 pts) PROGRAM readarray IMPLICIT NONE REAL, ALLOCATABLE :: A(:) INTEGER :: N, i OPEN(unit=10, file= array.in, status= old, action= read ) READ(10,*), N ALLOCATE(A(N)) do i = 1, N READ(10,*) A(i) end do CLOSE(10) PRINT *, A! just to test DEALLOCATE(A) END PROGRAM readarray pg pts: 12 tot pts: 34

CS1073 Exam 3, Fall 2009 page 5 5. Write a code fragment that uses a loop to sum all the elements of the following array: REAL :: A(2000) You must also print the sum once it is computed. (6 pts) REAL :: mysum INTEGER :: i mysum = 0.0 DO i = 1, 2000 mysum = mysum + A(i) PRINT *, SUM of A =, mysum 6. Write a code fragment that computes and prints out the number of strictly negative elements in the array declared as: INTEGER :: IA(800) (10 pts) INTEGER :: i, nneg nneg = 0 DO i = 1, 800 IF (IA(i).LT. 0) & nneg = nneg + 1 PRINT *, Number of negative values in IA is:, nneg pg pts: 16 tot pts: 50

CS1073 Exam 3, Fall 2009 page 6 7. Write a code fragment that copies an M-row by N-column piece of the double precision 2-D array A into the 2-D array B using loops (i.e., you cannot use array subsections). You may assume the following declarations: DOUBLE PRECISION :: A(1000,800), B(600,2000) INTEGER, PARAMETER :: M=400, N=600 (10 pts) INTEGER :: i, j DO j = 1, N DO i = 1, M B(i,j) = A(i,j) 8. Do the same copy, but this time use array subsections in your code fragment to perform the copy (i.e., you cannot use loops at all for this question). (4 pts) B(1:M,1:N) = A(1:M,1:N) pg pts: 14 tot pts: 64

CS1073 Exam 3, Fall 2009 page 7 9. Assume the following array declarations: REAL :: A(1000), POSA(1000), NEGA(1000) Assume A has been initialized to random values. Further, assume there are npos positive numbers in A, and nneg negative numbers in A. Write a code fragment that computes nneg, npos, and places all the positive numbers in A in the first npos entries of POSA, and places all negative numbers in A in the first nneg entries of NEGA. For this problem, zero will be counted as a positive number. (10 pts) INTEGER :: i, npos, nneg npos = 0 nneg = 0 DO i = 1, 1000 IF (A(i).LT. 0.0) THEN nneg = nneg + 1 NEGA(nneg) = A(i) ELSE npos = npos + 1 POSA(npos) = A(i) ENDIF pg pts: 10 tot pts: 74

CS1073 Exam 3, Fall 2009 page 8 10. Write a code fragment including a loop (but only one loop!) that indexes a REAL 1-D array of unknown size (your code should discover the size), and at the end prints out three values computed in this loop: (A) The sum of every number less than -1.0 in the array (B) The product of every strictly positive number (> 0) in the array (C) The number of array elements that are exactly zero You should assume the array index begins at 1, and that the array name is X. (16 pts). INTEGER :: N, nzeros REAL :: mysum, myprod N = size(x) mysum = 0.0 myprod = 1.0 nzeros = 0 DO i = 1, N IF (X(i).LT. -1.0) THEN mysum = mysum + X(i) ELSE IF (X(i).GT. 0.0) THEN myprod = myprod * X(i) ELSE IF (X(i).EQ. 0.0) THEN nzeros = nzeros + 1 END IF PRINT *, Number of zeros in X is:, nzeros PRINT *, Sum of all numbers less than -1 in X is:, mysum PRINT *, Product of all strictly positive numbers in X is:, myprod pg pts: 16 tot pts: 90

CS1073 Exam 3, Fall 2009 page 9 11. Write a code fragment that sorts the array A into greatest-to-least order, assuming the following declaration: REAL :: A(100) (10 pts) REAL :: bigval INTEGER :: imax, i, j DO i = 1, 99 bigval = A(i) imax = i DO j = i+1, 100 IF (A(j).GT. bigval) THEN bigval = A(j) imax = j END IF IF (i.ne. imax) THEN A(imax) = A(i) A(i) = bigval END IF......... OR......... DO i = 1, 99 bigval = A(i) DO j = i+1, 100 IF (A(j).GT. bigval) THEN bigval = A(j) A(j) = A(i) A(i) = bigval END IF pg pts: 10 tot pts: 100