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

Similar documents
CSE 303 Midterm Exam

Solution for Data Structure

CSE 333 Midterm Exam 7/29/13

CSE 333 Midterm Exam 5/9/14 Sample Solution

C0MP1911 Final Exam 1337 Computing 1

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Introduction to Computer Science Midterm 3 Fall, Points

Kurt Schmidt. October 30, 2018

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

Multiple Choice Questions ( 1 mark)

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

CS 0449 Sample Midterm

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

CSE 333 Midterm Exam July 24, Name UW ID#

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

ECE264 Fall 2013 Exam 3, November 20, 2013

C: How to Program. Week /Mar/05

Subject: Fundamental of Computer Programming 2068

CSE 333 Midterm Exam 7/25/16. Name UW ID#

Linked Lists and other Dynamic Data Structures

Common Misunderstandings from Exam 1 Material

CS 2301 Exam 3 B-Term 2011

Ashish Gupta, Data JUET, Guna

ESC101N: Fundamentals of Computing End-sem st semester

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Technical Questions. Q 1) What are the key features in C programming language?

CSE 333 Midterm Exam 5/10/13

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Program Organization and Comments

Understanding Pointers

Chapter 2 - Introduction to C Programming

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

PROGRAMMAZIONE I A.A. 2017/2018

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

ECE264 Spring 2013 Final Exam, April 30, 2013

Midterm Exam 2 Solutions, C programming

ECE264 Spring 2014 Exam 2, March 11, 2014

Lab 4: Tracery Recursion in C with Linked Lists

CS61, Fall 2012 Section 2 Notes

CMSC 212 Midterm #2 (Fall 2005) ANSWERS AND GRADING KEY

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

Programming and Data Structures

Computer Systems and Networks

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Programming. Lists, Stacks, Queues

Fundamentals of Programming Session 4

ECE264 Fall 2013 Exam 1, September 24, 2013

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

Chapter 10: File Input / Output

Approximately a Test II CPSC 206

Module III. Linear Data Structures and their Linked Storage Representation

C programming for beginners

This is an open book, open notes exam. But no online or in-class chatting.

Arrays, Strings, & Pointers

Linked Lists in C and C++

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Dynamic memory allocation

CSE 303 Winter 2008 Midterm Key

Pointers and scanf() Steven R. Bagley

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Have examined process Creating program Have developed program Written in C Source code


CS16 Exam #1 7/17/ Minutes 100 Points total

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

BLM2031 Structured Programming. Zeyneb KURT

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CSE 374 Midterm Exam 11/2/15. Name Id #

Arithmetic Expressions in C

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

The Hyderabad Public School, Begumpet, Hyderabad, A.P

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

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

DC54 DATA STRUCTURES DEC 2014

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

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

CS270 Homework 5 (HW5)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Computer Systems Assignment 2: Fork and Threads Package

Transcription:

CLOSED BOOK Exam #1 100 Points NAME 1. The following program has (at least) 10 syntax errors. Circle each error. Write the corrected program in the blank space below. 2 points for each error you find. -2 points for circling something that s OK. (20 points total.) #include stdio.h void main( int j;k; j = 0;; for (j>10 ; do j++) if j==7 then printf(j is equal to 7); else k = j exit 100; Page 1

CLOSED BOOK Exam #1 100 Points 2. Using the integer variables x, y, and z, write a single statement for each of the following tasks. (2 points each, 20 points total.) a) Square x, place result in y b) Divide y by 17, place result in z. c) Add 1 to x (leave result in x) d) Divide z by 7, leave remainder in x. (Discard quotient) e) Multiply y by 3. (leave result in y) f) Add x, y, and z together, leave result in x g) Store a 1 in x if y is greater than z, store a 0 in x otherwise h) Subtract x from y, leave result in y. i) Change the sign of z. (Leave result in z.) j) Subtract x from y, leave result in z. Page 2

CLOSED BOOK Exam #1 100 Points 3. Write for statements to do the following. Use the integer variables x, y, and z in addition to the loop index i (if necessary). (4 points each, 20 points total). a) Add up the integers from 3 to 17 inclusive. b) Add up the EVEN integers from 3 to 17 inclusive. c) Add up the digits of x, base 10. The result for 123 would be 6. d) Print numbers that are powers of 2, starting with 1 and ending with 2048. DO NOT use a loop index. e) Print the characters of the string MyString, one character per line. Page 3

CLOSED BOOK Exam #1 100 Points 4. Write single if statements (if/else OK) to do the following tasks. (4 points each, 20 points total.) a) Add x to z if y is greater than ten. b) Print X is odd or X is even depending on whether x is odd or even. (Remainder by 2). c) Print String is too long if the string MyString has more than 20 characters, not counting the \0 character. d) Print File is at end if the end-of-file has been encountered for the file MyFile. e) Add 1 to x if y is equal to zero, add 2 to x otherwise. Page 4

CLOSED BOOK Exam #1 100 Points 5. Write a whole program that reads one line of text containing last name, first name, and middle initial (without period), in that order. Assume that the three parts of the name are separated by spaces. Print the name in the following order and quit: First Name, Middle Initial (with period), Last Name. (10 points) 6. Write a whole program that reads words from a file. Assume the file is named words.txt. Assume the file is in the current directory. Assume there is one word per line in the file. Count the number of times that the word red appears in the file. Don t count things like Red or RED. Count lower case only. (10 points) Page 5

CLOSED BOOK Exam #1 100 Points NAME 1. The following program has (at least) 10 syntax errors. Circle each error. Write the corrected program in the blank space below. 2 points for each error you find. -2 points for circling something that s OK. (20 points total.) #include stdio.h void main( int j;k; j = 0;; for (j>10 ; do j++) if j==7 then printf(j is equal to 7); else k = j exit 100; #include <stdio.h> // missing angle-brackets void main( ) int j,k; // missing right paren // comma between j and k j = 0;; for ( ; j>10 ; j++) // two semicolons needed // do must be removed if (j==7) // incorrect if syntax printf("j is equal to 7"); // quotes needed else k = j; // semicolon required // closing brace missing exit(100); // missing parens around argument Page 1

CLOSED BOOK Exam #1 100 Points 2. Using the integer variables x, y, and z, write a single statement for each of the following tasks. (2 points each, 20 points total.) a) Square x, place result in y y = x * x; b) Divide y by 17, place result in z. z = y / 17; c) Add 1 to x (leave result in x) x++; d) Divide z by 7, leave remainder in x. (Discard quotient) x = z % 7; e) Multiply y by 3. (leave result in y) y *= 3; f) Add x, y, and z together, leave result in x x += y + z; g) Store a 1 in x if y is greater than z, store a 0 in x otherwise x = y > z; h) Subtract x from y, leave result in y. y -= x; i) Change the sign of z. (Leave result in z.) z = -z; j) Subtract x from y, leave result in z. z = y - x; Page 2

CLOSED BOOK Exam #1 100 Points 3. Write for statements to do the following. Use the integer variables x, y, and z in addition to the loop index i (if necessary). (4 points each, 20 points total). a) Add up the integers from 3 to 17 inclusive. for (i=3,x=0 ; i<18 ; i++) x += i; b) Add up the EVEN integers from 3 to 17 inclusive. for (i=4,x=0 ; i<17 ; i+=2) x += i; c) Add up the digits of x, base 10. The result for 123 would be 6. for (y=0 ; x>0 ; x /= 10) y += x % 10; d) Print numbers that are powers of 2, starting with 1 and ending with 2048. DO NOT use a loop index. for (x=1 ; x<2049 ; x *= 2) printf("%d\n",x); e) Print the characters of the string MyString, one character per line. for (x=0 ; MyString[x] ; x++) printf("%c\n",mystring[x]); Page 3

CLOSED BOOK Exam #1 100 Points 4. Write single if statements (if/else OK) to do the following tasks. (4 points each, 20 points total.) a) Add x to z if y is greater than ten. if (y>10) z += x; b) Print X is odd or X is even depending on whether x is odd or even. (Remainder by 2). if (x%2) printf("x is odd\n"); else printf("x is even\n"); c) Print String is too long if the string MyString has more than 20 characters, not counting the \0 character. if (strlen(mystring) > 20) printf("string is too long\n ); d) Print File is at end if the end-of-file has been encountered for the file MyFile. if (feof(myfile)) printf("file is at end\n"); e) Add 1 to x if y is equal to zero, add 2 to x otherwise. if (y == 0) x++; else x += 2; Page 4

CLOSED BOOK Exam #1 100 Points 5. Write a whole program that reads one line of text containing last name, first name, and middle initial (without period), in that order. Assume that the three parts of the name are separated by spaces. Print the name in the following order and quit: First Name, Middle Initial (with period), Last Name. (10 points) #include <stdio.h> #include <stdlib.h> void main() char First[20],Middle[20],Last20]; scanf("%s %s %s",last,first,middle); printf("%s %s. %s\n",first,middle,last); 6. Write a whole program that reads words from a file. Assume the file is named words.txt. Assume the file is in the current directory. Assume there is one word per line in the file. Count the number of times that the word red appears in the file. Don t count things like Red or RED. Count lower case only. (10 points) #include <stdio.h> #include <stdlib.h> void main() int Count; char Word[20]; FILE *InFile; InFile = fopen("words.txt","r"); if (InFile == NULL) printf("can t open words.txt\n"); exit(100); Count = 0; scanf("%s",word); while (!feof(infile)) if (strcmp(word,"red") == 0) Count++; scanf("%s",word); printf("red Count = %d\n",count); Page 5

NAME 1. Suppose you want to read a collection of strings from a file and store those strings in a singly linked list. You don t know how long the strings will be. Some will be short, and some will be very long. You want to store each string using as little storage as possible. Show the data structure that would be used to create this linked list. (10 Points) 2. Assume that you want to do the same thing as in problem 1, but now you want to create a doubly linked list. Show the data structure that would be used to create a doubly linked list for the same problem. (10 Points) Page 1

3. Assume that you are creating a singly linked-list using the following data structure. The singly linked list will be a stack, using the variables given below. In the spaces given below, show how to allocate a new structure and link it into the stack. (10 Points) // definitions typedef struct abc struct abc *Next; int First; int Second; int Third; ABC; ABC *Head = NULL, *Temp; int i,j,k; int main(int argc,char *argv[]) // Assume that the following code is part of a loop // read three integers scanf( %d %d %d,&i,&j,&k); // Allocate a new structure here and copy i, j, and k into it // Link the structure into the stack here Page 2

4. Assume that you are creating a singly linked-list using the following data structure. The singly linked list will be a queue, using the variables given below. In the spaces given below, show how to allocate a new structure and link it into the queue. (10 Points) // definitions typedef struct abc struct abc *Next; int First; int Second; int Third; ABC; ABC *Head = NULL, *Tail=NULL, *Temp; int i,j,k; int main(int argc,char *argv[]) // Assume the following code is part of a loop // read three integers scanf( %d %d %d,&i,&j,&k); // Allocate a new structure here and copy i, j, and k into it // Link the structure into the queue here Page 3

5. You have created a singly linked list using the following definitions. Write a loop that frees all the storage for the linked list. (10 Points) typedef struct ABC struct ABC *Next; char *First; char *Second; ABC; ABC *Head=NULL, *Tail=NULL, *Temp, *Temp2; int main(int argc,char *argv[]) // write your loop here Page 4

6. You have created a singly linked list using the following definitions. You now wish to delete the first item from the list that has a Value of 10. Show the loop that would be used to FIND THE ITEM. DO NOT delete the item. (10 Points) typedef struct ABC struct ABC *Next; int Value; ABC; ABC *Head=NULL,*Tail=NULL,*Prev, *Temp; int main(int argc, char *argv[]) // Write your loop here 7. Using the same structures as for the previous problem, write a loop to add 10 to the Value component of each structure in the list. (10 Points) int main(int argc, char *argv[]) // Write your loop here Page 5

8. Using the following array definitions, write a loop that adds 10 to each element of the array. USE A POINTER to access the array elements. (10 Points) int main(int argc,char *argv[]) int Abc[20]; int Size=20; int *Temp; int i; // Write Your Loop Here 9. Using the following definitions, write a segment of code that opens a file named abc.txt reads every character of the file (getc function) and displays the characters on standard out. Close the file when you are done. Exit the program if the file doesn t exist. (10 Points) int main(int argc,char *argv[]) FILE *MyFile; char tc; // Write Your Code Here Page 6

10. Add #define statements to the beginning of this program, so that the rest of the code will compile properly. Define things in a way that makes the program read correctly. (10 Points) // Add your #define statements here int main(int argc,char *argv[]) int i,j,k; i += TWO; j *= FIVE; k = j / TEN; Extra Credit: Go back to problem 6, and examine the data structures. In the space below, write code to verify that the item has been found, and delete it from the list. Note: the curve will be computed based on the scores on the previous 10 problems. Extra credit will be added AFTER the curve is computed. (10 points) Page 7

NAME 1. Suppose you want to read a collection of strings from a file and store those strings in a singly linked list. You don t know how long the strings will be. Some will be short, and some will be very long. You want to store each string using as little storage as possible. Show the data structure that would be used to create this linked list. (10 Points) typedef struct abc struct abc *Next; char *Value; ABC; or struct abc struct abc *Next; char *Value; ; 2. Assume that you want to do the same thing as in problem 1, but now you want to create a doubly linked list. Show the data structure that would be used to create a doubly linked list for the same problem. (10 Points) typedef struct abc struct abc *Next; struct abc *Prev; char *Value; ABC; or struct abc struct abc *Next; struct abc *Prev; char *Value; ; Page 1

3. Assume that you are creating a singly linked-list using the following data structure. The singly linked list will be a stack, using the variables given below. In the spaces given below, show how to allocate a new structure and link it into the stack. (10 Points) // definitions typedef struct abc struct abc *Next; int First; int Second; int Third; ABC; ABC *Head = NULL, *Temp; int i,j,k; int main(int argc,char *argv[]) // Assume that the following code is part of a loop // read three integers scanf( %d %d %d,&i,&j,&k); // Allocate a new structure here and copy i, j, and k into it Temp = (ABC *)malloc(sizeof(abc)); if (Temp == NULL) exit(100); // possibly with error message Temp->First = i; Temp->Second = j; Temp->Third = k; // Link the structure into the stack here Temp->Next = Head; Head = Temp; Page 2

4. Assume that you are creating a singly linked-list using the following data structure. The singly linked list will be a queue, using the variables given below. In the spaces given below, show how to allocate a new structure and link it into the queue. (10 Points) // definitions typedef struct abc struct abc *Next; int First; int Second; int Third; ABC; ABC *Head = NULL, *Tail=NULL, *Temp; int i,j,k; int main(int argc,char *argv[]) // Assume the following code is part of a loop // read three integers scanf( %d %d %d,&i,&j,&k); // Allocate a new structure here and copy i, j, and k into it Same as for problem 3. // Link the structure into the queue here if (Head == NULL) Head = Temp; else Tail->Next = Temp; Temp->Next = NULL; Tail = Temp; Page 3

5. You have created a singly linked list using the following definitions. Write a loop that frees all the storage for the linked list. (10 Points) typedef struct ABC struct ABC *Next; char *First; char *Second; ABC; ABC *Head=NULL, *Tail=NULL, *Temp, *Temp2; int main(int argc,char *argv[]) // write your loop here for (Temp = Head ; Temp ; Temp = Temp2) Temp2 = Temp->Next; free(temp->first); // assuming dynamic allocation of strings free(temp->second); free(temp); Page 4

6. You have created a singly linked list using the following definitions. You now wish to delete the first item from the list that has a Value of 10. Show the loop that would be used to FIND THE ITEM. DO NOT delete the item. (10 Points) typedef struct ABC struct ABC *Next; int Value; ABC; ABC *Head=NULL,*Tail=NULL,*Prev, *Temp; int main(int argc, char *argv[]) // Write your loop here for (Temp = Head,Prev=NULL; Temp && Temp->Value!= 10; Prev=Temp,Temp=Temp->Next); 7. Using the same structures as for the previous problem, write a loop to add 10 to the Value component of each structure in the list. (10 Points) int main(int argc, char *argv[]) // Write your loop here for (Temp = Head ; Temp ; Temp=Temp->Next) *Temp += 10; Page 5

8. Using the following array definitions, write a loop that adds 10 to each element of the array. USE A POINTER to access the array elements. (10 Points) int main(int argc,char *argv[]) int Abc[20]; int Size=20; int *Temp; int i; // Write Your Loop Here for (i=0,temp=abc ; i<20 ; I++,Temp++) *Temp += 10; 9. Using the following definitions, write a segment of code that opens a file named abc.txt reads every character of the file (getc function) and displays the characters on standard out. Close the file when you are done. Exit the program if the file doesn t exist. (10 Points) int main(int argc,char *argv[]) FILE *MyFile; char tc; // Write Your Code Here MyFile = fopen("abc.txt","r"); if (MyFile == NULL) exit(100); // perhaps with error message tc = getc(myfile); while (!feof(myfile)) putc(tc,myfile); tc = getc(myfile); fclose(myfile); Page 6

10. Add #define statements to the beginning of this program, so that the rest of the code will compile properly. Define things in a way that makes the program read correctly. (10 Points) // Add your #define statements here #define TWO 2 #define FIVE 5 #define TEN 10 int main(int argc,char *argv[]) int i,j,k; i += TWO; j *= FIVE; k = j / TEN; Extra Credit: Go back to problem 6, and examine the data structures. In the space below, write code to verify that the item has been found, and delete it from the list. Note: the curve will be computed based on the scores on the previous 10 problems. Extra credit will be added AFTER the curve is computed. (10 points) if (Temp!= NULL) if (Prev == NULL) Head = Temp->Next; else Temp->Prev = Temp->Next; // This is enough for full credit if (Temp->Next == NULL) Tail = Prev; // many other correct solutions // not necessary to free Temp Page 7