AMCAT Automata Coding Sample Questions And Answers

Similar documents
Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

What we have learned so far

C: How to Program. Week /Mar/05

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

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

Chapter 2 - Introduction to C Programming

Functions. Arash Rafiey. September 26, 2017

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

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

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

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

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Decision Making and Loops

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

Fundamentals of Programming Session 4

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

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

Decision Making -Branching. Class Incharge: S. Sasirekha

COMP s1 Lecture 1

CC112 Structured Programming

Subject: Fundamental of Computer Programming 2068

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Unit 7. Functions. Need of User Defined Functions

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

C Program Development and Debugging under Unix SEEM 3460

Lab 3. Pointers Programming Lab (Using C) XU Silei

Data Types and Variables in C language

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

High Performance Computing

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Lecture 2: C Programming Basic

Prepared by: Shraddha Modi

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Chapter 1 & 2 Introduction to C Language

User Defined Functions

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

C Programming Review CSC 4320/6320

Array Initialization

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Control Structure: Loop

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

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

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

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

ESC101N: Fundamentals of Computing End-sem st semester

More Arrays. Last updated 2/6/19

Introduction to Computing Lecture 01: Introduction to C

BSM540 Basics of C Language

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

DECISION CONTROL AND LOOPING STATEMENTS

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

CS113: Lecture 4. Topics: Functions. Function Activation Records

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

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

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

Overview (1A) Young Won Lim 9/14/17

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Computer Systems Lecture 9

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Overview (1A) Young Won Lim 9/9/17

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Chapter 2: Overview of C. Problem Solving & Program Design in C

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

IV Unit Second Part STRUCTURES

ESC101N Fundamentals of Computing

ET156 Introduction to C Programming

Fundamentals of Programming Session 14

Principles of Programming. Chapter 6: Arrays

Overview (1A) Young Won Lim 9/25/17

Arrays and Pointers (part 1)

ET156 Introduction to C Programming

Preview from Notesale.co.uk Page 6 of 52

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

C introduction: part 1

3.3 Structures. Department of CSE

UNIT - V STRUCTURES AND UNIONS

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Fundamental of Programming (C)

Why VC++ instead of Dev C++?

CSE / ENGR 142 Programming I

Arrays. Systems Programming Concepts

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Fundamentals of C Programming

Problem: Read 10 numbers from the keyboard and store them

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

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

2. Numbers In, Numbers Out

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

Transcription:

1) Find the syntax error in the below code without modifying the logic. #include <stdio.h> int main() float x = 1.1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return 0; Answer: #include <stdio.h> int main() int x = 1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return 0; The expression used in the switch must be an integral type (int, char, and enum). Any other type of expression is not allowed. 2) Find the logical error in the below code. void main () int i, j, n = 5; for(i=1; i<n; i++)

for(j=i;j<n;j++); printf( %d, i); printf( \n ); Solution: void main () int i, j, n = 5; for(i=1; i<n; i++) for(j=i;j<n;j++) printf( %d, i); printf( \n ); we use a semicolon in C statement to tell the compiler where s the end of our statement. Second for loop executes one time. 3) Complete the below code by reusing the existing function. Find the index of equilibrium element in the given array. In an array equilibrium element is the one where the sum of all the elements to the left side is equal to the sum of all the elements in the right side. Return Value: 1) Return -1 if no equilibrium element is found 2) In case there is more than one equilibrium element, return the element with least index value. You are required to complete the given code by reusing the existing function. You can click on Compile & run anytime to check the compilation/execution status of the program you can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all the test cases. Code approach For the question: You will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods.

Test Case: a[] = 1,2,3,4,3,3. 4 is the equilibrium element since its left side sum (1+2+3) is equal to its right side sum (3+3) #include <stdio.h> // Return the sum of elements from index 0 to (idx 1) int left_side_sum(int a[], int n, int idx) for(i = 0; i < idx; i++) // Return the sum of elements from index (idx + 1) to (n 1) int right_side_sum(int a[], int n, int idx) for(i = idx + 1; i < n; i++) // returns -1 if no equilibrium index found int findequilibriumindex(int a[], int n) // Type your code here int main()

//code int a[10], n, i; // get the elements count scanf( %d, &n); // get the array elements for(i=0; i<n; i++) scanf( %d, &a[i]); int equiindex = findequilibriumindex(a, n); if(equiindex!= -1) printf( %d, a[equiindex]); return 0; Solution: // Return the sum of elements from index 0 to (idx 1) int left_side_sum(int a[], int n, int idx) for(i = 0; i < idx; i++) // Return the sum of elements from index (idx + 1) to (n 1) int right_side_sum(int a[], int n, int idx)

for(i = idx + 1; i < n; i++) // returns -1 if no equilibrium index found int findequilibriumindex(int a[], int n) // Type your code here int i; for(i = 0; i < n; i++) if(left_side_sum(a, n, i) == right_side_sum(a, n, i)) return i; return -1; int main() //code int a[10], n, i; // get the elements count scanf( %d, &n); // get the array elements for(i=0; i<n; i++) scanf( %d, &a[i]);

int equiindex = findequilibriumindex(a, n); if(equiindex!= -1) printf( %d, a[equiindex]); return 0;