Structured programming

Similar documents
Structured programming

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

CS 177. Lists and Matrices. Week 8

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

Programming in C Lab

Object oriented programming

Structured programming

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

'C' Programming Language

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Structured programming

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

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

C Programming Language

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

BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER

Fundamental of Programming (C)

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Government Polytechnic Muzaffarpur.

Programming Language A

Structured programming

What we have learned so far

Computer Science & Engineering 150A Problem Solving Using Computers

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

CS 223: Data Structures and Programming Techniques. Exam 2

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions)

ECE 15 Fall 15 Final Solutions

WAP 10. WAP 11. WAP 12. WAP 13. WAP 14. WAP 15. WAP 16. WAP 1. : 17. WAP 18. WAP 19. WAP 20. WAP 21. WAP 22. WAP 23. WAP & 24. WAP

Principles of Programming. Chapter 6: Arrays

Arrays in C. By Mrs. Manisha Kuveskar.

ESC 101N: Fundmentals of Computing ( IInd Semester) Mid Sem II Examination PM, Monday 7th March, 2011

Structured programming. Exercises 3

UNIT III ARRAYS AND STRINGS

AMCAT Automata Coding Sample Questions And Answers

Subject: Fundamental of Computer Programming 2068

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

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Module 4: Decision-making and forming loops

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

Sorting & Searching. Hours: 10. Marks: 16

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

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Structured programming

List of Programs: Programs: 1. Polynomial addition

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

Module 6: Array in C

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

Chapter 7 Solved problems

1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code

MTH 307/417/515 Final Exam Solutions

Programming Language A

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Basic Assignment and Arithmetic Operators

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

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

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

One Dimension Arrays 1

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

ESC101N: Fundamentals of Computing End-sem st semester

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

F.E. Sem. II. Structured Programming Approach

CSCI 2132 Software Development. Lecture 19: Generating Permutations

Programming Language B

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

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Indian Institute of Technology, Kharagpur

Problem: Read 10 numbers from the keyboard and store them

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Programming Language A

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

Worksheet 11 Multi Dimensional Array and String

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

PDS Class Test 2. Room Sections No of students

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Array Lesson 1 Outline

Arrays and Applications

DECISION CONTROL AND LOOPING STATEMENTS

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

Question Bank (SPA SEM II)

Solutions to Assessment

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

Principles of C and Memory Management

Programming Language B

Write a C program using arrays and structure

Fundamental of Programming (C)

Content. In this chapter, you will learn:

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

CME111 Programming Languages I. Week 10 Arrays. Assist. Prof. Dr. Caner ÖZCAN

Functions. Arash Rafiey. September 26, 2017

Array. Arrays. Declaring Arrays. Using Arrays

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

CGS 3460 Summer 07 Midterm Exam

Transcription:

Exercises 6 Version 1.0, 25 October, 2016

Table of Contents 1. Arrays []................................................................... 1 1.1. Declaring arrays......................................................... 1 1.2. Accessing array element................................................. 1 2. Problems.................................................................... 1 2.1. Problem 1............................................................... 1 2.2. Problem 2............................................................... 2 2.3. Problem 3............................................................... 3 2.4. Problem 4............................................................... 3 2.5. Problem 5............................................................... 4 2.6. Problem 6............................................................... 5 2.7. Problem 7............................................................... 5 3. Matrices (two dimensional arrays)............................................ 6 3.1. Problem 1............................................................... 6 3.2. Problem 2............................................................... 6 3.3. Problem 3............................................................... 7 4. Source code of the examples and problems..................................... 9

1. Arrays [] 1.1. Declaring arrays type variable_name[size]; int a[10]; float x[99]; char c[5]; 1.2. Accessing array element array[element_index]; int a[10]; a[0] = 1; // assigning value 1 of the first element printf("%d", a[9]); // printing the value of the last element 2. Problems 2.1. Problem 1 Write a program that for two arrays read from SI will check if they are equal. Print out the result from the comparison. The maximum size of arrays is 100. 1. Arrays [] 1

Solution p6_1_en.c #include<stdio.h> int n1, n2, element, i; int a[max], b[max]; printf("first array size: "); scanf("%d", &n1); printf("second array size: "); scanf("%d", &n2); if (n1!= n2) printf("arrays are equal\n"); else { printf("elements of the first array: \n"); for (i = 0; i < n1; ++i) { printf("a[%d] = ", i); printf("elements of the second array: \n"); for (i = 0; i < n2; ++i) { printf("b[%d] = ", i); scanf("%d", &b[i]); // check if arrays are equal: for (i = 0; i < n1; ++i) if (a[i]!= b[i]) break; if (i == n1) printf("arrays are equal\n"); else printf("arrays are not equal\n"); 2.2. Problem 2 Write a program that for an array read from SI, will compute the sum of even elements, the sum of odd elements and will compute the ratio even/odd. Example For array: 3 2 7 6 2 5 1 The program should print: Sum even: 8 Sum odds: 16 Ratio: 0.75 2 2.2. Problem 2

Solution p6_2_en.c #include <stdio.h> int i, n, a[max], count_even = 0, count_odd = 0, sum_even = 0, sum_odd = 0; { if (a[i] % 2) { count_odd++; sum_odd += a[i]; else { count_even++; sum_even += a[i]; printf("sum even: %d\nsum odd: %d\n", sum_even, sum_odd); printf("ratio: %.2f\n", (float)count_even / count_odd); 2.3. Problem 3 Write a program that will compute the scalar product of two vectors with n coordinates. The number of coordinates n and the coordinates are read from SI. Print the result on the SO. Solution p6_3_en.c #include<stdio.h> int a[max], b[max], n, i, scalar = 0; scanf("%d", &b[i]); scalar += a[i] * b[i]; printf("the scalar product is: %d\n", scalar); 2.4. Problem 4 Write a program that will check if a given array with n elements read from SI is ascending, descending or neither. Print the result. 2.3. Problem 3 3

Solution p6_4_en.c #include <stdio.h> int n, element, a[max], i; short ascending = 1, descending = 1; for (i = 0; i < n - 1; ++i) { if (a[i] >= a[i + 1]) { ascending = 0; break; for (i = 0; i < n - 1; ++i) { if (a[i] <= a[i + 1]) { descending = 0; break; if (!descending &&!ascending) printf("array is not ascending and not descending\n"); else if (descending) printf("array is descending\n"); else if (ascending) printf("array is ascending\n"); 2.5. Problem 5 Write a program that will rotate elements of an array for one place in right. Solution p6_5_en.c #include<stdio.h> int n, i; int a[max]; for(i = 0; i < n; ++i) { int temp = a[n-1]; for(i = n - 1; i > 0; i--) { a[i] = a[i-1]; a[0] = temp; for(i = 0; i < n; ++i) { printf("%d ", a[i]); 4 2.5. Problem 5

2.6. Problem 6 Write a program that will rotate elements of an array for m places in right. Solution p6_6_en.c #include<stdio.h> int n, i; int a[max]; int m; scanf("%d", &m); for(i = 0; i < n; ++i) { int j; for(j = 0; j < m; j++) { int temp = a[n-1]; for(i = n - 1; i > 0; i--) { a[i] = a[i-1]; a[0] = temp; printf("\n"); for(i = 0; i < n; ++i) { printf("%d ", a[i]); 2.7. Problem 7 Write a program that will remove duplicate from an array. After the transformation print the array. 2.6. Problem 6 5

Solution p6_5_en.c #include <stdio.h> int a[max], n, i, j, k, removed = 0; for (i = 0; i < n - removed; ++i) for (j = i + 1; j < n - removed; ++j) if (a[i] == a[j]) { for (k = j; k < n - 1 - removed; ++k) a[k] = a[k + 1]; removed++; --j; n -= removed; printf("%d\t", a[i]); 3. Matrices (two dimensional arrays) 3.1. Problem 1 On matrix read from SI compute the difference of sum of elements of odd columns and sum of elements of even rows. Print the result. Solution p7_1.c #include<stdio.h> int a[max][max], n, m; int i, j, sumcols = 0, sumrows = 0; scanf("%d %d", &n, &m); for (j = 0; j < m; ++j) scanf("%d", &a[i][j]); for (j = 0; j < m; ++j) { if ((j + 1) % 2) sumcols += a[i][j]; if (!((i + 1) % 2)) sumrows += a[i][j]; printf("%d", sumcols - sumrows); 3.2. Problem 2 Write a program that for a given matrix read from SI will replace the elements from the main diagonal with the difference between the maximum and minimum element 6 3. Matrices (two dimensional arrays)

from the matrix. Print the result matrix. Solution p7_2.c #include <stdio.h> int a[max][max]; int n; int i, j; { for (j = 0; j < n; ++j) { scanf("%d", &a[i][j]); if (i == 0 && j == 0) { min = max = a[i][j]; else if (a[i][j] > max) { max = a[i][j]; else if (a[i][j] < min) { min = a[i][j]; { a[i][i] = max - min; { for (j = 0; j < n; ++j) { printf("%d\t", a[i][j]); printf("\n"); 3.3. Problem 3 Write a program that will print on screen if a given matrix is symetric based on the main diagonal. Dimensions and the matrix are read from SI. 3.3. Problem 3 7

Solution p7_3.c #include <stdio.h> int main () { int a[max][max], n, i, j, is_symmetrical = 1; for (j = 0; j < n; ++j) scanf ("%d", &a[i][j]); for (i = 0; i < n - 1; ++i) { for (j = i + 1; j < n; ++j) if (a[i][j]!= a[j][i]) { is_symmetrical = 0; break; if (!is_symmetrical) break; if (is_symmetrical) printf("symmetrical\n"); else printf("not symmetrical\n"); 8 3.3. Problem 3

4. Source code of the examples and problems https://github.com/finki-mk/sp/ Source code ZIP 4. Source code of the examples and problems 9