ECE 15 Fall 15 Final Solutions

Similar documents
ECE 15 Fall 15 Midterm Solutions

ECE 15 Fall 16 Midterm Solutions

ECE264 Fall 2013 Exam 1, September 24, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

Name Roll No. Section

ECE264 Summer 2013 Exam 1, June 20, 2013

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : Mid-Semester Examination

Structured programming

MTH 307/417/515 Final Exam Solutions

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Functions. Arash Rafiey. September 26, 2017

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

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

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Solutions to Assessment

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

'C' Programming Language

ESC101N: Fundamentals of Computing End-sem st semester

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

Worksheet 4 Basic Input functions and Mathematical Operators

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

York University Faculty Science and Engineering Fall 2008

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Answer all questions. Write your answers only in the space provided. Full marks = 50

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Midterm Exam

CSCI 2132 Software Development. Lecture 19: Generating Permutations

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

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

only in the space provided. Do the rough work in the space provided for it. The question paper has total 12 pages.

Today s Learning Objectives

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Computer Science & Engineering 150A Problem Solving Using Computers

EECE.2160: ECE Application Programming Fall 2017

Programming Language B

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

CSC 1107: Structured Programming

Lecture06: Pointers 4/1/2013

Subject: Fundamental of Computer Programming 2068

Arrays. Here is the generic syntax for an array declaration:

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Chapter 13 Control Structures

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

File: /home/young/mywork/5.cordic_vhdl/f.c/lut.src.print Page 1 of 9

Make sure the version number is marked on your scantron sheet. This is Version 1

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

Chapter 13. Control Structures

Write a C program using arrays and structure

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Question Bank (SPA SEM II)

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING PROGRAMMING TECHNIQUES I EE271

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CSC 1107: Structured Programming

Arrays. Systems Programming Concepts

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

Arrays and Strings. Arash Rafiey. September 12, 2017

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

ECE264 Spring 2013 Exam 1, February 14, 2013

EECE.2160: ECE Application Programming Fall 2016 Exam 3 Solution

COS 126 MIDTERM 1, FALL

More examples for Control statements

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Control Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action;

EECE.2160: ECE Application Programming Fall 2017

ECE15: Introduction to Computer Programming Using the C Language Lecture 6: Arrays

Midterm Exam 2 Solutions, C programming

#include <stdio.h> #include <math.h> int shownum(int digits[], int digitcount);

Programming & Data Structure Laboratory. Day 2, July 24, 2014

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : End-Semester Examination

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Lecture 9 - C Functions

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

COS 126 General Computer Science Spring Written Exam 1

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Principles of C and Memory Management

Principles of Programming. Chapter 6: Arrays

Arrays and Pointers (part 2) Be extra careful with pointers!

Dept. of CSE, IIT KGP

EECE.2160: ECE Application Programming Fall 2017

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

ECE264 Spring 2013 Final Exam, April 30, 2013

Introduction to Scientific Computing and Problem Solving

Transcription:

ECE 15 Fall 15 Final Solutions This is a closed-book exam: no notes, books, calculators, cellphones, or friends are allowed. In problems 2 7, you can assume that the user s input is correct. User input is underlined. If you need more space, please use the blank pages in the back. You have 3 hours. Good luck! Name: PID: A Points 1. /20 2. /13 3. /13 4. /13 5. /13 6. /13 7. /15 = /100

Problem 1 (20 points) For each of the following programs, indicate whether or not it has errors, including syntax-errors and infinite loops. If there are errors, mark all of them (there may be more than one) on the program and briefly describe the problems. If there are no errors, write the program s output. Hint: two programs contain errors. (1) #include <stdio.h> int arr[][2] = {1,2,3,4; printf("%d,%d", arr[1][0], arr[0][1]); Output: 3, 2 (2) #include <stdio.h> static char s[15] = "Tritons"; int i = 0; char ch; ch = s[++i]; printf("%c", ch); ch = s[i++]; printf("%c", ch); ch = s[i++]; ch += 1; printf("%c", ch); Output: r r j (3) #include <stdio.h> void myprint(); myprint(); void myprint() { char c = k ; if(c!= \n ) myprint(); printf("%c", c); No Output: X

(4) #include <stdio.h> int i = 0; void f(); f(); printf("%d", i); f(); void f() { static int z = 8; printf("%d", z); z++; i++; Output: 8 1 9 (5) #include <stdio.h> int myfunc(int); int i = myfunc(10); printf("%d", --i); int myfunc(int x) { int i; for (i = 1; i < x; i++) { if (i == 4) break; return (i++); Output: 3 (6) #include <stdio.h> #define FUNC(x) x*x/(3+x) float t = 6, a; a = FUNC(t+1); printf("%.1f", a); Output: 1 2 1

(7) #include <stdio.h> int x = 3, y = 2; int *z = &x; printf("%d", y**z*x+*z); Output: 2 1 (8) #include <stdio.h> void f(char*); char arr[4] = { a, b, c, d ; f(arr[0]); void f(char *a) { a++; printf("%c", *a); a++; printf("%c", *a); return a; No Output: X (9) #include <stdio.h> char *s; s = "%s"; printf(s, "%s"); Output: % s (10) #include <stdio.h> int arr[3] = {2, 3, 4; int *p; p = arr; printf("%d,", *p); p = p+1; printf("%d", *p); Output: 2, 3

Problem 2 (13 points) Write a program that takes as command line arguments three positive integers X,Y, and Z. The program calculates the smallest and largest of the three elements MIN = min{x, Y, Z and MAX = max{x, Y, Z, and prints their product MAX * MIN. 3 8 5 MAX * MIN = 24 2 2 2 MAX * MIN = 4 1 2 10 MAX * MIN = 10 4 3 3 MAX * MIN = 12 Hint: Recall that main takes two arguments, int argc and char *argv[]. The latter is an array of strings, hence each of its elements is a string. You may want to use the functions sscanf or atoi (we expect you to remember how to use them). #include <stdio.h> int main(int argc, char* argv[]) { int X, Y, Z, MAX, MIN; X = atoi(argv[1]); Y = atoi(argv[2]); Z = atoi(argv[3]); MAX = X>Y? X : Y; MAX = MAX>Z? MAX : Z; MIN = X<Y? X : Y; MIN = MIN<Z? MIN : Z; printf("max * MIN = %d\n", MAX*MIN);

Problem 3 (13 points) Write a program that asks the user for a positive integer n and evaluates f(n) for a function f defined as 3 n = 1, f(n) = 5 n = 2, 2 f(n 1) f(n 2) n > 2. Your program should include the following recursive function. - int myfunc(int n); that takes the integer n and returns f(n). n: 1 f(1) = 3 n: 2 f(2) = 5 n: 3 f(3) = 7 n: 10 f(10) = 21 #include <stdio.h> int myfunc(int); printf("n: "); scanf("%d", &x); printf("f(%d) = %d\n",x,myfunc(x)); int myfunc(int n) { if (n==1) return 3; if (n==2) return 5; else return 2*myFunc(n-1) - myfunc(n-2);

Problem 4 (13 points) Write a program that asks the user of a positive integer, multiplies the digits at the same distance from the left and right, namely, the first and the last digits, the second and the second-last ones, etc. and returns the sum of these products. For example, if the input is 840251, it prints (8*1)+(4*5)+(0*2), namely 28. If the integer has an odd number of digits, then the center digit is not multiplied by itself. For example, for 14532 the program prints (1*2)+(4*3)+5, namely 19. You may assume that the input has at most 10 digits. Number: 1742 Sum: 30 Number: 1 Sum: 1 Number: 18723 Sum: 26 #include <stdio.h> int a[10]; int number, n_digits, sum = 0; printf("number: "); scanf("%d", &number); int x = number; int i = 0; while (x > 0) { n_digits +=1; a[i] = x%10; i++; x /= 10; for (int j = 0; j<n_digits/2; j++) { sum += a[j]*a[n_digits-j-1]; if (n_digits % 2 == 1) sum += a[n_digits/2]; printf("sum: %d\n", sum);

Problem 5 (13 points) For security purposes, we want to encode words by breaking them into two parts after a given number of characters, reversing the characters in the first part, and then printing the second part followed by the reversed first part. For example, for the word nocart and breaking index 2, the two parts are no and cart, the first part reversed is on, and the encoded word is carton. Write a program that asks the user of a word of length at most 20, the integer breaking index (starting at 1), and prints the encoded message. Word: starwar Breaking index: 4 Encoded word: warrats Word: noemotic Breaking index: 2 Encoded word: Emoticon Word: LoveMom Breaking index: 4 Encoded word: MomevoL #include <stdio.h> char a[20], e[20]; int b, length, i=0; printf("word: "); while (1) { a[i] = getchar(); if (a[i]== \n ) break; i++; printf("breaking index: "); scanf("%d", &b); length = i; for (int j = b; j < length; j++) { e[j-b]=a[j]; for (int j = 0; j < b; j++) { e[j+length-b]=a[b-j-1]; printf("encoded word: "); for (int j=0; j< length; j++) { printf("%c", e[j]); printf("\n");

Problem 6 (13 points) Write a program that opens an input-file input.txt whose first line specifies the size of a square matrix. The subsequent lines consist of space-separated double elements of a square matrix, each row in a separate line. The program reads the matrix from the input-file and calculates the mean of all matrix elements, namely their sum divided by the number of elements. It then subtracts the mean from all the matrix elements and prints the resulting matrix with one-digit precision in an output file output.txt. For example in the example shown below the mean 6.2 is subtracted from each matrix element. Assume that the input file exists and has the right format. more input.txt 3 4.2 2.1 3.8 9.4 5.2 6.5 7.6 8.3 9.1 more output.txt -2.0-4.1-2.4 3.2-1.0 0.3 1.4 2.1 2.9 #include <stdio.h> int x, dim; FILE *fin = fopen("input.txt", "r"); FILE *fout = fopen ("output.txt", "w"); fscanf(fin,"%d",&dim); double ar[dim][dim]; double mean, sum = 0; for (int i=0; i <dim ; i++) { for (int j=0;j<dim; j++) { fscanf(fin,"%lf",&ar[i][j]); sum = sum + ar[i][j]; mean = sum / (dim * dim); for (int i=0;i<dim;i++) { for (int j=0; j<dim;j++) { fprintf(fout,"%.1lf",ar[i][j]-mean); if (j!= dim-1) fprintf(fout," "); fprintf(fout,"\n"); fclose(fin); fclose(fout);

Problem 7 (15 points) The pattern of a string of characters indicates the order at which characters appear. The first character observed is marked as 1, the second as 2, etc. For example the pattern of cat and dog is 123. The pattern of ece and mom is 121. The pattern of aaa is 111, of cool is 1223, of banana is 123232, and of aardvark is 11234125. Write a program that asks the user of a string of at most 20 lower-case characters and outputs its pattern. Your program should include the function below. - void pattern(char word[], int pat[], int length); that takes character array word[], integer array pat[], and the length of the pattern as inputs, and stores the pattern in pat[]. In the main function of the program you should find the length of the pattern and call the function pattern. Word: egg Pattern: 122 Word: banana Pattern: 123232 Word: abracadabra Pattern: 12314151231

#include <stdio.h> void pattern(char [], int [], int length); char a[20]; int pat[20]; pat [0] = 1; printf("word: "); int i = 0; while (1) { a[i] = getchar(); if (a[i] == \n ) break; i++; int l = i; pattern(a, pat, l); printf("pattern: "); for (int i = 0; i < l; i++) { printf("%d", pat[i]); printf("\n"); void pattern(char a[], int pat[], int length) { int flag, temp, cur = 2; for (int i = 1; i < length; i++) { flag = 1; for (int j = 0; j< i; j++) { if (a[i] == a[j]) { flag = 0; temp = pat[j]; break; if (flag == 1) { pat[i] = cur; cur++; else pat[i] = temp;