0% found this document useful (0 votes)
28 views21 pages

IP Prefinal-4

Uploaded by

siddiq.shaik24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views21 pages

IP Prefinal-4

Uploaded by

siddiq.shaik24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Jan - 2024

I B.Tech I Sem Regular Examinations


INTRODUCTION TO PROGRAMMING
Subject Code: 21CIMAT3010
Time: 3 hours Max. Marks: 70

Instructions:
1. Answer all the questions from Part-A. Each question carries Two mark.
2. Answer one full question from each unit in Part-B. Each full question carries 10 marks
-----------------------------------------------------------------------------------------------------------------
PART- A
1 a Define compiler 1 CO1 [2M]

b How to define constants in C. 2 CO1 [2M]

c What is loop with an example. 1 CO2 [2M]

d Write the syntax for if-else leader. 2 CO2 [2M]

e Differentiate between 1D and 2D arrays 1 CO3 [2M]

f List syntax for any 4 string library functions. 2 CO3 [2M]

g What is a Pointer? 1 CO4 [2M]


h What is the syntax to crate structure. 1 CO4 [2M]
i Write the syntax to define user defined function. 1 CO5 [2M]
j Write short notes on files. 1 CO5 [2M]

PART- B
2 a Explain the generations of computers. 2 CO1 [5M]

b What is type casting (Type conversion)? Explain different typesof type 1 CO1 [5M]
conversions with example?

OR
3 a Define Flowchart? Draw and explain different symbols used in flowchart 2 CO1 [5M]
with example?

b Explain the basic organization of a computer with a neat diagram 2 CO1 [5M]

4 a Write a C program to check whether a number is Prime or not 2 CO2 [5M]

b Difference between while and do-while. 2 CO2 [5M]

OR
5 a Write a C program to given number is Armstrong number or not? 3 CO2 [5M]

b Explain for loop with example. 2 CO2 [5M]


6 a Define an array and explain one dimensional array with 2 CO3 [5M]
example

b Write a c program to find length of string without using strlen() function. 2 CO3 [5M]

OR
7 a Write a c program to find multiplication of arrays. 2 CO3 [5M]

b Explain string handling functions. 2 CO3 [5M]

8 a How to declare and initialize structures? Explain with examples 3 CO4 [5M]

b Explain definition and syntax of union. 2 CO4 [5M]

OR
9 a Write a program to copy string using pointers? 2 CO4 [5M]

b What is pointer arithmetic? Explain. 2 CO4 [5M]

1 a Discuss different modes of files in c. 2 CO5 [5M]


0
b Give syntax for function prototype, return statement and function call? 2 CO5 [5M]

OR
1 a Write a c program to copy the content of one file to another. 3 CO5 [5M]
1
b Explain call by reference with example. 2 CO5 [5M]

*****

SCHEME OF VALUATION

NAME OF EXAMINATION: I B.Tech I Semester Regular Examinations, JAN-2024


NAME OF SUBJECT: INTRODUCTION TO PROGRAMMING
SUBJECT CODE:
NAME AND OFFICIAL ADDRESS OF THE EXPERT: G.Saraswathi
CONTACT NUMER: 9121634510
QUESTION PAPER SET NO: 1
Q.N Sub Marks
Detailed Key & Scheme of valuation
o Q.No allotted
1 a Define compiler. 2M
Compiling is the process computers use to translate high-level
programming languages into computer-understandable machine
language. A compiler is the name of the software that performs this
conversion.
Source code is the format in which programmers create their
programs. Before a program can be executed, the source code
should go through several steps. The source code must go through a
compiler to convert the high-level language instruction into object
code.

b Using #define preprocessor directive: This directive is used to 2M


declare an alias name for existing variable or any value.
Syntax: #define identifierName value Each type
– identifierName: It is the name given to constant. carries 1M
– value: This refers to any value assigned to
identifierName.
using a const keyword: Using const keyword to define constants
is as simple as defining variables as constants.
syntax: const datatype constant_name;
eg: const int SIDE = 10;

c What is loop with an example? 2M


Loops in programming are used to repeat a block of code until the
specified condition is met. A loop statement allows programmers to
execute a statement or group of statements multiple times without
repetition of code.
Example:
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 3; i++)
{
printf( "Hello World\n");
}
return 0;
}
Output:
Hello World
Hello World
Hello World
d Syntax: 2M
if(test expression1 is true)
statement1;
else if(test expression2 is true)
statement2;
else if(test expression3 is true)
statement3;
……..
e One-Dimensional Array Two-Dimensional Array 2M
A one-dimensional array A two-dimensional array
stores a single list of various stores an array of various Write any
elements having a similar data arrays, or a list of various lists, four
type. or an array of various one-
dimensional arrays.
It represents multiple data It represents multiple data
items in the form of a list. items in the form of a table
that contains columns and
rows.
It has only one dimension. It has a total of two
dimensions.
One can easily receive it in a The parameters that receive it
pointer, an unsized array, or a must define an array’s
sized array. rightmost dimension.
Total number of Bytes = The Total number of Bytes = The
size of array x the size of array size of array visible or
variable or datatype. datatype x the size of second
index x the size of the first
index.

f Explanation any 4 string library functions 2M


strlen( ) Gives the length of str1
strcat( ) Concatenates str2 at the end of str1 Write any
strcpy( ) Copies str2 into str1 four
strcmp( ) Returns 0 if str1 is same as str2.
Returns <0 if strl < str2.
Returns >0 if str1 > str2.
strrev( ) Reverses the given string
g A pointer is a variable that refers to the address of a value. It 2M
makes the code optimized and makes the performance fast.
Whenever a variable is declared inside a program, then the system
allocates some memory to a variable. The memory contains some
address number. The variables that hold this address number is
known as the pointer variable.
Example:
#include <stdio.h>
int main()
{
int *p; //pointer of type integer.
int a=5;
p=&a;
printf("Address value of 'a' variable is %u",p);
return 0;
}
h struct structure_name 2M
{
Data_member_type data_member_defination;
Data_member_type data_member_defination;
Data_member_type data_member_defination;
...
...
}(structure_variables);
i 1. return_type function_name( parameter list) 2M
2. {
3. body of the function
4. }

j File handing in C is the process in which we create, open, read, 2M


write, and close operations on a file. C language provides
different functions such as fopen(), fwrite(), fread(), fseek(),
fprintf(), etc. to perform input, output, and many different C file
operations in our program.

PART-B
Mar
Q. Sub ks
Detailed Key & Scheme of valuation
No Q.No allott
ed
2. a) 5M
Generations of
Computer Time-Period Evolving Hardware
Each
First gener
1940s – 1950s Vacuum Tube Based
Generation ation
1M
Second
1950s – 1960s Transistor Based 5*1=
Generation
5M

Third
1960s – 1970s Integrated Circuit Based
Generation

Fourth
1970s – Present Microprocessor Based
Generation

Fifth
Present – Future Artificial Intelligence Based
Generation

b) Type Casting: In typing casting, a data type is converted into another 5M


data type by the programmer using the casting operator during the
program design. In typing casting, the destination data type may be Defi
smaller than the source data type when converting the data type to nitio
n 1M
another data type, that’s why it is also called narrowing conversion.

Syntax/Declaration:- Each
type
destination_datatype = (target_datatype)variable; carri
es
In C programming, we can convert the value of one data type (int, float, 2M
double, etc.) to another. This process is known as type conversion. 2*2=
4M
This type of conversion is known as implicit type conversion. In C, there
are two types of type conversion:

 Implicit Conversion
 Explicit Conversion

Implicit Type Conversion:

As mentioned earlier, in implicit type conversion, the value of one type is


automatically converted to the value of another type.

Example:

#include<stdio.h>

int main()

// create a double variable

double value = 4150.12;

printf("Double Value: %.2lf\n", value);

// convert double value to integer

int number = value;

printf("Integer Value: %d", number);

return 0;

Output:
Double Value: 4150.12

Integer Value: 4150

Explicit Type Conversion:

In explicit type conversion, we manually convert values of one data type


to another type. For example,

Example:

#include<stdio.h>

int main()

// create an integer variable

int number = 35;

printf("Integer Value: %d\n", number);

// explicit type conversion

double value = (double) number;

printf("Double Value: %.2lf", value);

return 0;

Output:

Integer Value: 35

Double Value: 35.00

3. a) Flowchart is a graphical representation of an algorithm. The pictorial 5M


representation of algorithm is called flowchart. A flowchart is a picture,
which shows the sequence in which data are read, Computing is Defi
nitio
performed, decisions are made and results are obtained. It makes use of
n 1M
the basic operations in programming. All symbols are connected among
themselves to indicate the flow of information and processing.
Each
Name Symbol Purpose type
carri
Terminal Start / Stop / Begin / End es
1M
oval
Diag
Input / output Input / Output of data ram=
Parallelogram 2
Exa
mple
Any processing to be
=2
Process performed can be
Rectangle represented

Decision operations that


determine which of the
Decision box
Diam alternative paths to be
ond followed

Used to connect different


Connector Circle parts of flowchart

Arrows Joins 2 symbols and also


Flow
represents flow of execution

Example: Flowchart for average of three numbers

b) Explain the basic organization of a computer with a neat diagram 5M

The computer is a combination of hardware and software. Hardware is


the physical Defi
nitio
Component of a computer while the software is the set of programs or n 1M
instructions. Both hardware and software together make the computer Diag
system function. Every task given to a computer follows an Input- ram
Process- Output Cycle (IPO cycle). =1
Expl
anati
on=3
The functional components of a computer:

1. Input Unit: The input unit is used to feed any form of data to the
computer.

Example: Keyboard, mouse, etc.

2. Central Processing Unit: The CPU is the major component that


interprets and executes software instructions. It also controls the
operation of all other components such as memory, input, and output
units.

The CPU has three components which are the Control unit, the
Arithmetic, and logic unit (ALU), and the Memory unit.

3. Arithmetic and Logic Unit: The ALU is a part of the CPU where
various computing functions are performed on data. The ALU performs
arithmetic operations such as addition, subtraction, multiplication,
division, and logical operations.

4. Control Unit: The control unit controls the flow of data between the
CPU, memory, and I/O devices. It also controls the entire operation of a
computer.

5. Output Unit: An Output Unit is any hardware component that conveys


information to users in an understandable form. Example: Monitor
Printer etc.

6. Memory Unit: The Memory Unit is of two types which are primary
memory and secondary memory. The primary memory is used to
temporarily store the programs and data when the instructions are ready
to execute. The secondary memory is use to store the data permanently.
The primary memory is volatitle, that is the content is lost when the
power supply is switched off. The Random Access Memory (RAM) is an
example of the main memory.

The Secondary memory is nonvolatile, that is, the content is available


even after the power supply is switched off. Hard disk. CD-ROM, and
DVD ROM are examples of secondary memory.
4. a) Program: 5M
#include <stdio.h> Progr
int main() am=
3
{ Outp
out=
int n, i, flag = 0;
2
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Output:
Enter a positive integer: 2
2 is not a prime number.
b) While Do While 5M
This loop will execute the statement(s)
It checks the condition first and
at least once, then the condition is
then executes statement(s) Writ
checked.
While loop allows initialization of Do while loop allows initialization of e any
counter variables before starting counter variables before and after 5
the body of a loop. starting the body of a loop. differ
ences
It is an entry controlled loop. It is an exit controlled loop.
5*1=
We do not need to add a We need to add a semicolon at the end 5M
semicolon at the end of a while of the while condition.
condition.
In case of a single statement, we
Brackets are always needed.
do need to add brackets.
In this loop, the condition is
The loop condition is specified after
mentioned at the starting of the
the block is executed.
loop.
5. a) Program: 5M
#include <stdio.h>
void main() Progr
am=
{
3
int num,r,sum=0,temp; Outp
printf("Input a number: "); our=
scanf("%d",&num); 2
for(temp=num;num!=0;num=num/10)
{
r=num % 10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number.\n",temp);
else
printf("%d is not an Armstrong number.\n",temp);
}
Output:
Input a number: 153
153 is an Armstrong number.
b) When you know exactly how many times you want to loop through a 5M
block of code, use the for loop instead of a while loop:
for (statement 1; statement 2; statement 3) { Defi
// code block to be executed nitio
} n 1M
Statement 1 is executed (one time) before the execution of the code
block. Synt
Statement 2 defines the condition for executing the code block. ax
Statement 3 is executed (every time) after the code block has been 2M
executed.
The example below will print the numbers 0 to 4:
int i; Exa
mple
for (i = 0; i < 5; i++) { 2M
printf("%d\n", i);
}
6. a) 5M
Array Definitions:
Defi
An array is collection of homogeneous elements that are represented
under a single variable name. (or) nitio
n 1M
An array is collection of same data type elements in a single entity.
Synt
ax
It allocates sequential memory locations. 2M
Individual values are called as elements.

One-Dimensional Arrays: Progr


am
Declaration of One-Dimensional Array: Like any other variables, arrays 2M
must be declared before they are used. The general form of array
declaration is

Syntax: datatype array_name[size];

The data type specifies the type of element that will be contained in the
array, such as int, float, or char or any valid data type.

The array name specifies the name of the array.

The size indicates the maximum number of elements that can be stored
inside the array.

The size of array should be a constant value.

For example: int marks[10]; //integer array

Program:

#include <stdio.h>

int main()

int numbers[5] = {10, 20, 30, 40, 50};

for(int i=0; i<5; i++)

printf("numbers[%d] = %d\n", i, numbers[i]);

return 0;

Output:

numbers[0] = 10

numbers[1] = 20

numbers[2] = 30

numbers[3] = 40

numbers[4] = 50
b) #include <stdio.h> 5M
int main() {
char s[] = "Programming is fun"; Decl
int i; arati
on
for (i = 0; s[i] != '\0'; ++i); 1M

printf("Length of the string: %d", i); Intial


return 0; izatio
} n 1M
Output loop
Length of the string 18 2M

Outp
ut
1M
7. a) #include<stdio.h> 5M
#include<stdlib.h> Decl
int main(){ arati
on
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
1M
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
Intial
printf("enter the number of column="); izatio
scanf("%d",&c); n 1M
printf("enter the first matrix element=\n");
for(i=0;i<r;i++) Multi
{ plicat
ion
for(j=0;j<c;j++) Logi
{ c 2M
scanf("%d",&a[i][j]);
} Outp
} ut
1M
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

b) 5M
1. strcat() Function

The strcat() function in C is used for string concatenation. It will Writ


append a copy of the source string to the end of the destination string. e any
2. strlen() Function 5
5*1=
The strlen() function calculates the length of a given string. It doesn’t 5M
count the null character ‘\0’.
3. strcmp() Function

The strcmp() is a built-in library function in C. This function takes two


strings as arguments and compares these two strings lexicographically.

4. strcpy

The strcpy() is a standard library function in C and is used to copy one


string to another. In C, it is present in <string.h> header file.
5. strchr()

The strchr() function in C is a predefined function used for string


handling. This function is used to find the first occurrence of a character
in a string.
8. a) Structure in c is a user-defined data type that enables us to store the 5M
collection of different data types. Each element of a structure is called a
member. Structures ca; simulate the use of classes and templates as it can Expl
store various information
anati
Declaring structure variable on
2M
We can declare a variable for the structure so that we can access the
member of the structure easily. There are two ways to declare structure
variable: Progr
am
By struct keyword within main() function 2M
By declaring a variable at the time of defining the structure. Outp
Program: ut
1M
#include<stdio.h>
#include <string.h>
struct employee
{
int id;
char name[50];
}e1; //declaring e1 variable for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
b) A union is a special data type available in C that allows to store 5M
different data types in the same memory location. You can define a
union with many members, but only one member can contain a value at
any given time. Unions provide an efficient way of using the same Defi
memory location for multiple-purpose. nitio
Defining a Union n and
To define a union, you must use the union statement in the same way as synta
you did while defining a structure. The union statement defines a new x 2M
data type with more than one member for your program. The format of
the union statement is as follows −
union [union tag] { Exa
member definition; mple
member definition; 2M
...
member definition; Outp
} [one or more union variables]; ut
1M
9. a) Program: 5M
#include <stdio.h>
void swap(int *x,int *y)
{ Progr
int t; am=
3
t = *x; Outp
ut =2
*x = *y;
*y = t;
}
int main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);
printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);
swap(&num1,&num2);
printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);
return 0;
}
Output:
Enter value of num1: 100
Enter value of num2: 200
Before Swapping: num1 is: 100, num2 is: 200
After Swapping: num1 is: 200, num2 is: 100

b) Pointer arithmetic: 5M
• There are a few very limited mathematical operations that may
be performed on address data types, i.e. pointer variables. Each
• Most commonly, integers may be added to or subtracted from opera
addresses. tion
Operation Declaration Usage carri
es
1M
Assignment Int *p1,*p2 p1=p2(both should be
same type )
5*1=
Addition of integer Int I,*ptr; Ptr+I 5M

Subtraction of integer Int I,*ptr; Ptr-i

Comparision Int *ptr1,*pt2 Ptr1<ptr2

Subtraction of pointers Int *ptr1,*pt2 Ptr1-ptr2 (addition of


pointers is not possible)
10. a) 5M
File opening modes in C
Writ
File opening modes or access modes specify the allowed operations on e any
the file to be opened. They are passed as an argument to the fopen() 5
function. mode
s
Modes Description 5*1=
5M
Searches file. If the file is opened successfully fopen( )
loads it into memory and sets up a pointer that points to the
r
first character in it. If the file cannot be opened fopen( )
returns NULL.

Open for reading in text mode. If the file exists, its


w contents are overwritten. If the file doesn’t exist, a new file
is created. Returns NULL, if unable to open the file.

Searches file. If the file is opened successfully fopen( )


loads it into memory and sets up a pointer that points to the
a last character in it. It opens only in the append mode. If the
file doesn’t exist, a new file is created. Returns NULL, if
unable to open the file.
Searches file. It is opened successfully fopen( ) loads it
r+ into memory and sets up a pointer that points to the first
character in it. Returns NULL, if unable to open the file.

Open for both reading and writing in binary mode. If the


rb+
file does not exist, fopen( ) returns NULL.

Searches file. If the file exists, its contents are overwritten.


w+ If the file doesn’t exist a new file is created. Returns
NULL, if unable to open the file.

Open for both reading and writing in binary mode. If the


wb+ file exists, its contents are overwritten. If the file does not
exist, it will be created.

Searches file. If the file is opened successfully fopen( )


loads it into memory and sets up a pointer that points to the
a+ last character in it. It opens the file in both reading and
append mode. If the file doesn’t exist, a new file is created.
Returns NULL, if unable to open the file.

Open for both reading and appending in binary mode. If


ab+
the file does not exist, it will be created.

b) Function Declaration: 5M
A function declaration defines the name and return type of a function in a
program. Before using the function, we need to declare it outside of a Defi
main() function in a program. nitio
n 1M
Syntax:
return_data_type function_name ( data_type arguments) ;
Synt
Function Calling: x=1
Exa
A function call is an important part of the C programming language. It is mple
called inside a program whenever it is required to call a function. It is
only called by its name in the main() function of a program. We can pass =3
the parameters to a function calling in the main() function.
Syntax: Add(a, b) // a and b are the parameters
Example:
#include <stdio.h>
int add(int a, int b);
void main()
{
int sum;
int a, b;
printf(" Enter the first and second number \n");
scanf("%d %d", &a, &b);
sum = add(a, b); // call add() function
printf( "The sum of the two number is %d", sum);
}
int add(int n1, int n2) // pass n1 and n2 parameter
{
int c;
c = n1 + n2;
return c;
}
Output:
Enter the first and second number
5
6
The sum of the two number is 11
11. a) #include <stdio.h> 5M
#include <stdlib.h> // For exit()
int main(){ Decl
FILE *fptr1, *fptr2; arati
char filename[100], c; on
printf("Enter the filename to open for reading and
"); crati
scanf("%s",filename); ng a
// Open one file for reading file
fptr1 = fopen(filename, "r"); 2M
if (fptr1 == NULL){
printf("Cannot open file %s ", filename);
exit(0); Copy
} ing
printf("Enter the filename to open for writing conte
"); nt
scanf("%s", filename); 2M
// Open another file for writing
fptr2 = fopen(filename, "w"); Closi
if (fptr2 == NULL){ ng
printf("Cannot open file %s ", filename); files
exit(0); 1M
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF){
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("
Contents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}

b) 5M
 In call by reference, the address of the variable is passed into the
function call as the actual parameter. Defi
 The value of the actual parameters can be modified by changing nitio
n
the formal parameters since the address of the actual parameters is 2M
passed.
 In call by reference, the memory allocation is similar for both
formal parameters and actual parameters. All the operations in the
function are performed on the value stored at the address of the
actual parameters, and the modified value gets stored at the same
Progr
address. am
3M
#include<stdio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}

Guidelines for the preparation of Scheme of Valuation:


 Provide neat sketches and diagrams wherever necessary.
 For numerical problems, complete solution is to be provided with subdivision of
marks.
 For theory question, salient points are to be provided with subdivision of marks.

You might also like