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

Similar documents
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #05

Principles of Programming. Chapter 6: Arrays

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

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

Practice Sheet #07 with Solutions

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

Practice problems Set 2

Computer Science & Engineering 150A Problem Solving Using Computers

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Subject: Computer Science

COMP 2001/2401 Test #1 [out of 80 marks]

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

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

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

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

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

AMCAT Automata Coding Sample Questions And Answers

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

Lesson 7. Reading and Writing a.k.a. Input and Output

Solutions to Assessment

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Functions. Arash Rafiey. September 26, 2017

Write a C program using arrays and structure

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

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

Structured programming

Lab #10 Multi-dimensional Arrays

First of all, it is a variable, just like other variables you studied

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

CS 223: Data Structures and Programming Techniques. Exam 2

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

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

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

CGS 3460 Summer 07 Midterm Exam

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

Name Roll No. Section

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Arrays and Strings. Arash Rafiey. September 12, 2017

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

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Introduction to Computer Science Midterm 3 Fall, Points

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

How to declare an array in C?

provided. Use any other space for rough work. The question paper has total 8 pages. Rough Work

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

Name :. Roll No. :... Invigilator s Signature :.. CS/B.TECH (NEW)/SEM-2/CS-201/ BASIC COMPUTATION & PRINCIPLES OF COMPUTER PROGRAMMING

Columns A[0] A[0][0] = 20 A[0][1] = 30

Assignment 1 Clarifications

Module 6: Array in C

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

CS201- Introduction to Programming Current Quizzes

Principles of C and Memory Management

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

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

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

C Program Development and Debugging under Unix SEEM 3460

Programming for Engineers Arrays

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

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

ESC101N Fundamentals of Computing

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

Creating, Compiling and Executing

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Programming Language B

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

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

Q. 1 What will be the output of the following program? Justify your answer. [4] #include <stdio.h> main(){ int i=4, a[5]={1,2,3,4,5};

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

High Performance Computing

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Storage class and Scope:

POINTER & REFERENCE VARIABLES

LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

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

Procedural Programming

Worksheet 11 Multi Dimensional Array and String

Example: Pointer Basics

Computer Organization & Systems Exam I Example Questions

CS 0449 Sample Midterm

Midterm Exam 2 Solutions, C programming

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Goals of this Lecture

Programming in C++: Assignment Week 1

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Basic and Practice in Programming Lab7

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

At the end of this module, the student should be able to:

More Arrays. Last updated 2/6/19

IPC144 - Introduction to Strings. array of characters is an array, each element of which has a character data type.

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Transcription:

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments, assume necessary libraries have been included. In case, if you feel any discrepancy, kindly state relevant assumption with proper reason. Every question has only one correct option among given four options. If you feel none of the option is correct then state your answer with proper reason. 1. Figure out the output of the below code written in C Programming Language. State relevant Assumptions, if any, during int arr[2] = 10, 20, 30, 40, 50; printf("%d", arr[1]); (a) 20 (b) Error at Line 4 (c) 10 (d) 40 2. Figure out the output of the below code. State relevant assumption if any during int arr[2]; printf("%d ", arr[3]); printf("%d ", arr[-2]); (a) Any Random Values (b) Error at printf( %d,arr[3]); (c) Error at Line 5 (d) Assignment Required after Declaration of Array 3. Figure out the output of the below code. State relevant assumption if any during Page 1

int arr[5]; arr[0] = 5; arr[2] = -10; arr[3/2] = 2; arr[3] = arr[0]; printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]); (a) 5 2 10 5 (b) 2 10 5 2 (c) Error at line 7 (d) 4 5 10 2 5 4. Which of the following integer arrays among arr1, arr2, arr3 are terminated by a null character during storage in memory. State relevant assumption if any during Assume necessary libraries have been included. char arr1[] = "Five"; char arr2[5] = "Five"; char arr3[]= 'F', 'i', 'v', 'e'; (a) arr1 only (b) arr1 & arr2 (c) arr2 & arr3 (d) D arr 3 only 5. Which of the following declaration of integer arrays among a1, a2, a3 in the below code will cause error during compile time. State relevant assumption if any during Assume necessary libraries have been included. int a1[][][2] = 1, 2, 3, 4, 5, 6, 7, 8 ; int a2[][2] = 1,2,3,4; int a3[][2][2] = 1, 2, 3, 4, 5, 6, 7, 8 ; (a) a2 only Page 2

(b) a1 only (c) a2 & a3 both (d) No compile time error 6. Figure out the output of the below code. State relevant assumption if any during Assume Necessary libraries have been included. #include <stdio.h> char *s = "Harry Potter"; s[0] = 'K'; printf("%s", s); (a) Segmentation Fault on GCC (b) Karry (c) Potter (d) 4 Karry Potter 7. Figure out the output of the below code. State relevant assumption if any during Assume Necessary libraries have been included. char *str = "A for Apple " "B for Ball " "C for Cat"; puts(str); (a) Compilation Error at Line 4 (b) A for Apple B for Ball C for Cat (c) A for Apple " "B for Ball " "C for Cat (d) A for Apple 8. Figure out the output of the below code. State relevant assumption if any during Assume necessary libraries have been included. 1. 2. 3. void arr1[100]; 4. void *arr2[100]; 5. (a) No output but compiled and executed successfully (b) Error at Line 4 : error : declaration of arr2 as array of void pointers (c) 3 Error at Line 3: error : declaration of arr1 as array of void (d) None of the above Page 3

9. Let A be a square matrix of size n x n. Consider the following program. What is the expected output? C = 100 for i = 1 to n do for j = 1 to n do Temp = A[i][j] + C A[i][j] = A[j][i] A[j][i] = Temp - C for i = 1 to n do for j = 1 to n do Output(A[i][j]); (a) The Matrix A Itself (b) Transpose of matrix A (c) Adding 100 to the upper diagonal elements and subtracting 100 from diagonal elements of A (d) None of the Above 10. What is the output printed by the following program? void main() int A[]=0,1,2,3,4,5,6,7; int n=8, step = 2, i, j, k, l, temp; for(i=0;i<n-step;i++) for(j=i;j<i+step;j++) temp = A[j]; A[j] = A[j+1]; A[j+1] = temp; step = (step*2)- 1; for(i=0; i<n; i++) printf( %d,a[i]); 11. What is the output of the following program? Page 4 main() int a[5] = 5, 1, 15, 20, 25 ; int i, j, k; i = ++a[1]; j = a[1]++ ;

k = a[i++] ; printf ("%d,%d,%d", i, j, k); Problems for Programming Practice (After the successful studies of Lecture 04 (Arrays and Strings), the students are supposed to solve the following problems in C programming language.) 1. Write a program to read an integer 1D array containing 8 bits (0s, 1s) of an unsigned binary integer. The program should print the decimal value for the integer. 2. Read a 1D array containing n elements (n input by user) containing only 0s and 1s. Print the length of the longest run of 1s. For example, in the array 01011110011, the length is 4. 3. A fast scheme for evaluating a polynomial such as:, for a given value of (for example) is to arrange the computation as follows: and compute the result from the innermost brackets outwards. Write a program which reads the order of the polynomial N, the coefficients of the polynomial in ascending order of powers and then stores them in a float array. There are no missing terms in the polynomial. The program then reads a value of x, and computes the value of the polynomial for that x. [Hint: Use the fast method described above.] 4. Consider an integer array where each element has a value either 0 or 1. Write a program to rearrange the array such that all the 0s appear before the 1s.Write a program to read three integers and print the largest among them. 5. Read n integers and print the third largest among them. n is input by user. You may use an array to store all the input numbers. 6. Currency notes are available in the following seven denominations: Rs. 1, Rs. 2, Rs. 5, Rs. 10, Rs. 20, Rs. 50, and Rs. 100. You will first read an integer array (named purse[7]) of seven elements, where the array elements represents the number of notes of each denomination (in increasing order of denomination) available with you. You are also given an amount of money to be paid using these notes. The amount will be less than Rs. 1000. You have to pay the amount using minimum number of notes. Print the number of notes of each denomination which you will use to pay the amount. If the amount cannot be paid using the available notes, print Failed to Pay. 7. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example, "carthorse" is an anagram of "orchestra". Write a program which Page 5

reads two character strings of same length and prints whether they are anagrams of each other. 8. Write C functions to perform the following operations with two-dimensional arrays. i) Reading any two dimensional array elements. ii) Finding maximum and standard deviation of array elements. iii) Printing the transpose of a matrix Test these functions by calling them from the main program with 2d-arrays having maximum dimension 3 x 4. 9. Consider a three dimensional vector space over integer. Write C programs to perform the following vector operations. i. Dot product of two vectors. ii. Cross product of two vectors. --*-- Page 6