DS Assignment I. 1. Set a pointer by name first and last to point to the first element and last element of the list respectively.

Similar documents
C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

IV Unit Second Part STRUCTURES

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

CS 0449 Sample Midterm

CS 261 Data Structures. Introduction to C Programming

Final CSE 131B Spring 2004

Common Misunderstandings from Exam 1 Material

ECE264 Fall 2013 Exam 1, September 24, 2013

DS Assignment II. Full Sized Image

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

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

Ch. 3: The C in C++ - Continued -

Understanding Pointers

Exercise Session 2 Simon Gerber

Kurt Schmidt. October 30, 2018

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

4) In C, if you pass an array as an argument to a function, what actually gets passed?

CS 1713 Introduction to Programming II

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Approximately a Test II CPSC 206

Dynamic memory allocation

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

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

211: Computer Architecture Summer 2016

Subject: Fundamental of Computer Programming 2068

Chapter-8 DATA TYPES. Introduction. Variable:

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Computer Programming Unit 3

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

High Performance Programming Programming in C part 1

Memory Management. CSC215 Lecture

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Compiling and Running a C Program in Unix

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

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Basic and Practice in Programming Lab7

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Variation of Pointers

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Strings. Compare these program fragments:

Lesson #8. Structures Linked Lists Command Line Arguments

Introduction to C++ with content from

Procedural programming with C

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

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

Princeton University COS 217: Introduction to Programming Systems Spring 2009 Final Exam Answers

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

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

Unit IV & V Previous Papers 1 mark Answers

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

Dynamic memory allocation (malloc)

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

COMP 2355 Introduction to Systems Programming

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

C PROGRAMMING Lecture 5. 1st semester

C PROGRAMMING Lecture 4. 1st semester

Today s class. Finish review of C Process description and control. Informationsteknologi. Tuesday, September 18, 2007

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Array Initialization

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

Pointers. Pointers. Pointers (cont) CS 217

Recitation 2/18/2012

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5


C Programming Review CSC 4320/6320

ECE264 Spring 2013 Final Exam, April 30, 2013

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

CS201 Some Important Definitions

Dynamic Allocation in C

EL2310 Scientific Programming

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

ME964 High Performance Computing for Engineering Applications

Memory Organization. The machine code and data associated with it are in the code segment

Fundamental of Programming (C)

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

Binghamton University. CS-211 Fall Variable Scope

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

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

Binghamton University. CS-211 Fall Variable Scope

Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE

Memory Allocation in C

CS201- Introduction to Programming Current Quizzes

Discussion 3 Richard Guo Advanced C 01/28/09

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

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Model Viva Questions for Programming in C lab

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

Transcription:

DS Assignment I 1 Suppose an integer array by name list is declared of size N (ex: #define N 10 int list[n]; ) Write C statements to achieve the following: 1 Set a pointer by name first and last to point to the first element and last element of the list respectively int *first = list; int *last = list + N - 1; 2 Give an expression to compute the middle pointer in terms of N and first pointer int *middle = first + N/2; 3 Suppose the array is declared of size 100, but the actual size is less than 100 Further assume that the first and the last pointers are available Give an expression for actual size in terms of first and last pointers // Suppose 'first' is pointing to first element, and 'last' is pointing to last element int size = (int)(last - first); 4 The equivalent expression for *p++ // Note ++*p is equivalent to ++(*p) But *p++ increments the pointer, not the value pointed by p *p++ is equivalent to p = p + 1 p = p + 1; 5 Suppose the following C statements are to be executed Write the equivalent statements for last two C statements in terms of index notation int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10; int *ip1 = &a[3]; int *ip2; ip2 = ip1 +1; *(ip1+1) = 50; *(ip1-2) = 4;

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10; int *ip1 = &a[3]; // ip1 pointing to a[3] ie '4' int *ip2; ip2 = ip1 + 1; // ip2 pointing to a[4] ie '5' *(ip1+1) = 50; // set the value of a[3+1] or a[4] to '50' *(ip1-2) = 4; // set the value of a[3-2] or a[1] to '4' // hence the equivalent expressions are a[4] = 50; a[1] = 4; 2 Demonstrate 3 ways for defining constants in C language ( Hint: #define, const, enumerated data types) 1 C allows you to declare constants When you declare a constant it is a bit like a variable declaration except the value cannot be changed The const keyword is to declare a constant int const a = 1; const int b = 2; 2 The preprocessor #define is another more flexible method to define constants in a program #define TRUE 1 #define FALSE 0 #define CRAP_VOLUME 9001 3 enum is the abbreviation for ENUMERATE, and we can use this keyword to declare and initialize a sequence of integer constants For eg enum COLORS { COLOR_RED = 1, COLOR_GREEN = 2, COLOR_BLUE = 9, ; Here, COLORS is the name given to the set of constants - the name is optional Now, if you don't assign a value to a constant, the default value for the first one in the list is 0

3A Explain the realloc with syntax Why is it inefficient? The 'realloc' C library function changes the size of the memory block pointed to by the pointer passed to the function to a new size passed into the function void *realloc(void *ptr, int size) The realloc function has to copy the memory block to a new location (whose address is returned by the function) if the memory resizing operation cannot be done in one place, hence temporarily acquiring huge amounts of memory, making it less efficient to the malloc/free pair 3B Write a program in c to allocate memory dynamically as follows: 1 Write a function allocate_mem to perform dynamic memory allocation for a single integer and assign 10 to that integer in the function (default value) 2 Write a main function to invoke this function and print the value assigned by the function (Hint: Use pointers to pointers concept) #include <stdioh> #include <stdlibh> void allocate_mem (int **n) { *n = (int *)malloc(sizeof(int *)); **n = 10; int main(int argc, const char * argv[]) { int *pn; int **ppn; ppn = &pn; allocate_mem(ppn); printf("%d", **ppn); // Prints 10 return 0;

4A With a suitable example explain how pointers are useful in achieving inter function communication? What is the importance of pointers? A pointer is a variable whose value is the address of another variable, ie, direct address of the memory location Like any variable or constant, you must declare a pointer before you can use it to store any variable address When we pass an argument to a function, it s passed by it s value therefore any changes made to the value are only valid inside the scope of the function in which it s passed However when we pass a pointer into a function, an address to the memory location of an variable is passed Any changes made to the address inside the function is reflected everywhere in the program eg void swap (int a, int b) { int c = a; a = b; b = c; void swap_ptr (int *a, int *b) { int c = *a; *a = *b; *b = c; int main(int argc, const char * argv[]) { int a = 6, b = 9; int *p = &a, *q = &b; printf("%d\t%d\n", a, b); // Prints '6 9' swap(a, b); printf("%d\t%d\n", a, b); // Still prints '6 9' swap_ptr(p, q); printf("%d\t%d\n", a, b); // Now prints '9 6'

4B Write a function that reverses the elements of an array in place In other words, the last element must become the first; the second from last must become the second and so, on The function must accept one pointer value and size of the array The function must return void void reverse (int *arr, int n) { int *start = arr, i; for (i = n-1; start < arr + n/2; ++start, --i) { int c = *start; *start = *(arr + i); *(arr + i) = c; 5 Create a structure STUDENT where STUDENT creates a variable of structures: 1 DOB {day ( use pointer ), month, year, 2 STU_INFO {reg_no, name(use pointer), address, 3 COLLEGE {college_name(use pointer), university_name whereas structure types from 1 to 3 are declared outside the STUDENT independently Write a C program to 1 Initialize all structure (1 to 3) member variables through STUDENT 2 Read the information of the student using structure pointer variable and display the read information 3 How do you read and write members variables of DOB type if the pointer variable is created for DOB inside STUDENT and STUDENT variable is also a pointer variable #include <stdioh> typedef struct DOB { int *day; int month; int year; DOB_t; typedef DOB_t* DOB_p_t; // DOB_t pointer type

typedef struct STU_INFO { long reg_no; char *name; char *address; STU_INFO_t; typedef struct COLLEGE { char *college_name; char *university_name; COLLEGE_t; typedef struct STUDENT { DOB_p_t dob; STU_INFO_t sinfo; COLLEGE_t college; STUDENT_t; typedef STUDENT_t* STUDENT_p_t; // STUDENT_t pointer type int main(int argc, const char * argv[]) { STUDENT_p_t pstudent; // Initializing // SUPER FRAKKING IMPORTANT NOTE // ALLOCATE THE MEMORY TO EVERY GORRAM POINTER INSIDE THE STRUCTURE pstudent = (STUDENT_p_t)malloc(sizeof(STUDENT_t)); pstudent->sinfoname = (char *)malloc(sizeof(char *)); pstudent->sinfoaddress = (char *)malloc(sizeof(char *)); pstudent->dob = (DOB_p_t)malloc(sizeof(DOB_p_t)); pstudent->dob->day = (int *)malloc(sizeof(int *)); pstudent->collegecollege_name = (char *)malloc(sizeof(char *)); pstudent->collegeuniversity_name = (char *)malloc(sizeof(char *)); // Reading values printf("enter student name: "); scanf("%s", pstudent->sinfoname); printf("enter student reg number: "); scanf("%ld", &pstudent->sinforeg_no); printf("enter student address: "); scanf("%s", pstudent->sinfoaddress); printf("enter student DOB (dd/mm/yyyy): "); scanf("%d%d%d", pstudent->dob->day, &(pstudent->dob->month), &(pstudent->dob->year));

printf("enter student college name: "); scanf("%s", pstudent->collegecollege_name); printf("enter student university name: "); scanf("%s", pstudent->collegeuniversity_name); // Printing the stuff printf("\n\nname\t\treg NO\t\tADDRESS\t\tDOB (dd/mm/yyyy)\t \tcollege\t\tuniversity\t\t"); printf("\n\n%s\t\t%ld\t\t%s\t\t%d/%d/%d\t\t%s\t\t%s\t\t", pstudent->sinfoname, pstudent->sinforeg_no, pstudent->sinfoaddress, *(pstudent->dob->day), pstudent->dob->month, pstudent->dob->year, pstudent->collegecollege_name, pstudent->collegeuniversity_name); return 0; Output for reference: Enter student name: Derp Enter student reg number: 9669 Enter student address: Derpsville Enter student DOB (dd/mm/yyyy): 4 5 1994 Enter student college name: Derpston Enter student university name: HerpDerp NAME REG NO ADDRESS DOB (dd/mm/yyyy) COLLEGE UNIVERSITY Derp 9669 Derpsville 4/5/1994 Derpston HerpDerp