CSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists

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

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Assignment #4: Scalar Field Visualization 3D: Cutting Plane, Wireframe Iso-surfacing

Module 01 Processing Recap

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)

Module 01 Processing Recap. CS 106 Winter 2018

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Scan Converting Lines

Name: SID: LAB Section: Lab 6 - Part 1: Pixels and Triangle Rasterization

CSCE 210 Dr. Amr Goneid Exercises (1): Stacks, Queues. Stacks

/INFOMOV/ Optimization & Vectorization. J. Bikker - Sep-Nov Lecture 5: SIMD (1) Welcome!

COMPUTER APPLICATION

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

CSCE 110 PROGRAMMING FUNDAMENTALS

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CSCE 2014 Final Exam Spring Version A

Tema 6: Dynamic memory

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

C Pointers. 6th April 2017 Giulio Picierro

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

Introduction to C Language (M3-R )

M1-R4: Programing and Problem Solving using C (JAN 2019)

CSCE 110 PROGRAMMING FUNDAMENTALS

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

CS 543: Computer Graphics Lecture 3 (Part I): Fractals. Emmanuel Agu

Output Primitives Lecture: 3. Lecture 3. Output Primitives. Assuming we have a raster display, a picture is completely specified by:

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

CSc 372 Comparative Programming Languages

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

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

CS 4731: Computer Graphics Lecture 21: Raster Graphics: Drawing Lines. Emmanuel Agu

Assignment #3: Scalar Field Visualization 3D: Cutting Plane, Wireframe Iso-surfacing, and Direct Volume Rendering

Solution Notes. COMP 151: Terms Test

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

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

Raster Scan Displays. Framebuffer (Black and White)

Painter s HSR Algorithm

CSCE Practice Midterm. Data Types

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

5200/7200 Fall 2007 Concurrence theorems for triangles

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

Data Structures Unit 02

Low-Level C Programming. Memory map Pointers Arrays Structures

(4) Find the syntax error(s), if any, in the following program: #include main() int x[5],*y,z[5]; for(i=0;i<5;i++) x[i]=i; z[i]=i+3; y=z; x=y; (5) Rew

Test 1: CPS 100. Owen Astrachan. October 11, 2000

Pointers, Dynamic Data, and Reference Types

Signature: ECE 551 Midterm Exam


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

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

ESC101N: Fundamentals of Computing End-sem st semester

Computer Graphics. Lecture 2. Doç. Dr. Mehmet Gokturk

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

Subject: Fundamental of Computer Programming 2068

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

Scan Conversion. Lines and Circles

DC54 DATA STRUCTURES DEC 2014

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

POINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:

TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013

1. General Computer Questions

C Programming, Autumn 2013, Exercises for the Second Week

CSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid

Computer Programming

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

CSCE Practice Midterm. Data Types

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

Tokens, Expressions and Control Structures

struct Properties C struct Types

Question7.How many proper subsets in all are there if a set contains (a) 7 elements (b) 4 elements

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Chapter - 2: Geometry and Line Generations

Pointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications

Computational Geometry Lab: SEARCHING A TETRAHEDRAL MESH

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

The Math Class. Using various math class methods. Formatting the values.

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Types, Operators and Expressions

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Programming Studio #9 ECE 190


(X 2,Y 2 ) (X 1,Y 1 ) (X 0,Y 0 ) (X c,y c ) (X 3,Y 3 )

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

Chapter 7 Arithmetic

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Types, Operators and Expressions

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

C Language Advanced Concepts. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

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

C Multiple Choice Questions and answers MCQ with Ans.

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

ECE 3331, Dr. Hebert, Summer-3, 2016 HW 11 Hardcopy HW due Tues 07/19 Program due Sunday 07/17. Problem 1. Section 10.6, Exercise 3.

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC

Transcription:

CSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists Exercises on Structs (Solutions) (a) Define a struct data type location with integer members row, column Define another struct data type pixeltype with members: color (0 for black and 1 for white) and position (a struct of type location as defined above). (b) A 2-D array A [ ] [ ] is of type pixeltype and represents a black and white image of size (n x m). Implement a function flipud (A) that uses the above struct definitions to receive the image and return it upside down. struct location int row, column; ; struct pixeltype int color; location position; ; const int N =...; const M =...; int n = N; int m = M; void flipud (pixeltype A [ ] [M], int n, int m) pixeltype temp; for (i = 0; i < n/2; i++) for (j = 0; j < m; j++) temp = A[i][j]; A[i][j] = A[n-i-1][j]; A[n-i-1][j] = temp; Declare a struct type fraction for a fractional number with numerator num and a denominator denom, both are integers. Write a function fadd (x,y,z) to receive two fractional numbers (x) and (y) and return the fractional variable z = x + y according to the rule: If x = a / b and y = c / d, then z = (ad + cb) / (bd) struct fraction int num, denom; ; void fadd (fraction x, fraction y, fraction & z) z.num = x.num * y.denom + y.num * x.denom; z.denom = x.denom * y.denom;

Declare a struct type item for an item with weight and total price, both are of type float. Write a function maxval (x,y,z) to receive two items (x) and (y) and return the more valuable item (z), i.e. the item with the higher price per weight ratio. struct item float weight, price; ; void maxval (item x, item y, item & z) if ((x.price / x.weight) >= (y.price / y.weight)) z = x else z = y; Declare a struct type for a point in 3-D space with coordinates (x,y,z). Write a function distance(a,b) that receives two points (a) and (b) in 3-D space and returns the distance between them. struct point float x, y, z; ; float distance (point a, point b) float dx, dy, dz; dx = a.x b.x; dy =a.y b.y; dz =a.z b.z; return sqrt (dx*dx + dy*dy + dz*dz) Declare a struct type for a complex number with a real part and an imaginary part, both of type double. Write a function cmult (a,b,c) to receive two complex variables (a) and (b) and return the complex variable c = a*b according to the rule: If a = x1 + j y1 and b = x2 + j y2 then c = (x1.x2 y1.y2) + j (x1.y2 + x2.y1) struct complex double x, y; ; void cmult (complex a, complex b, complex &c) double Rec, Imc; Rec = a.x * b.x a.y * b.y; Imc = a.x * b.y + b.x * a.y; c.x = Rec; c.y = Imc;

A color image is stored as a 2-D array A[ ][ ] of pixels, with N rows and M columns. Each pixel A[i][j] is represented as a struct with three integer values representing its color components (Red, Green, Blue). To convert the pixel color to grey-level, we take the average of the three color components (rounded to the nearest integer) as the brightness. Write a function to receive such array and return another array B[ ][ ] containing the brightness of each pixel in the image. Consider N and M to be global constants. Solution const int N =...; const int M =...; struct pixel int red, green, blue; ; void Brightness (pixel A[ ][M], int B[ ][M]) int i, j, b; pixel p; for (i = 0; i < N; i++) for (j = 0; j < M; j++) p = A[i][j]; b = (p.red + p.green + p.blue)/3.0 + 0.5; // or b = floor((p.red + p.green + p.blue)/3.0 + 0.5); B[i][j] = b; Question 1 (15 points) A color image is stored as a 2-D array A[ ][ ] of pixels of N rows and M columns. Each pixel is represented as a struct with three integer values representing its color components (Red, Green, Blue). A sub-image is a block with the first H rows and first W columns, where H N and W M. Write a function to receive the image and the two integers H, W and return a pointer to a 1-D dynamic array containing the Green component of the sub-image defined by H and W. Consider N and M to be global constants. Declare a struct type item for an item with weight and total price, both are of type float. Consider the value of an item to be the ratio of price to weight. Write a recursive function to receive: a 1-D array of items, a starting index (s) an end index (e) a number (V) of type float a number (W) of type float The function should return the number of valuable items. We will define a valuable item as that having a weight less than (W) and a value greater than (V).

Declare a struct type item for an item with weight and total price, both are of type float. Consider the value of an item to be the ratio of price to weight. Write a function maxval to receive a 1-D array of items of size (N) and a number (k) of type float and return the item with the value nearest to (k).

Exercises on Dynamic Lists (Solutions) Assume the following declarations: struct node int info; node *next; node *p; Draw a diagram representing the nodes and pointers generated by the following code: node *h = NULL; int n = 1357; do p = new node; p-> info = n % 10; p -> next = h; h = p; n = n/10; while (n > 0); Final Result: h 1 3 5 7 Assume that the sequence of integers (17, 20, 50, 90) is already stored in a simple linked list of the structure given above in the order given ( first node contains 17). Trace the following: void What ( node *head ) node *p, *q, *r ; p = head ; q = NULL ; while (p!= NULL) r = q ; q = p ; p = p->ptr ; q->ptr = r ; head = q ; What will this function do if used on the above list? Left as an exercise. Assume the following declarations: struct node char info; node *next; node *p; node *h = NULL; node *c = h; Draw a diagram representing the nodes and pointers generated by the following code: string s = DATA LIST ; for (int i = 0; i < s.length( ); i++) p = new node; p-> info = s.at(i); p -> next = NULL; if (c == NULL) h = p; c = p; else c -> next = p; c = c -> next;

Solution The given code will insert the characters of the string s into a linked list of nodes in the same order, i.e., head BAT CAT DOG Given the above list and assuming the following declaration: struct node string info; node *ptr; Trace the following function to show the list after the function call: void What ( node *head ) node *p, *q, *r ; p = head ; q = NULL ; while (p!= NULL) r = q ; q = p ; p = p->ptr ; q->ptr = r ; head = q ; What is the objective of this function?