click on the link👇👇👇
MODEL QUESTION PAPER
PPS
(PROGRSMING FOR PROBLEM SOLVING)
F.Y.B.TECH
MARKS=100
download file link given at the bottom of this page
Q1. (a) list and explain formatted I/O statement in C
with syntax and
Example?
Ans:- Some of the most important
formatted console input/output functions are -
Functions |
Description |
scanf() |
|
printf() |
|
sscanf() |
|
print() |
|
Some of the most commonly
used format specifiers used in console formatted input/output functions are displayed in the
table below -
Format Specifiers |
Description |
%hi |
|
%hu |
|
%d |
|
%u |
|
%ld |
|
%lu |
|
%c |
|
%c |
|
%f |
|
%lf |
|
%Lf |
|
%s |
|
OR
(a) List the string handing standard library
function in C. explain the standerd library function which is used to compare
the two string ,along with syntax and C code?
Ans:-Few commonly used string handling function are discussed below:
function work of function
strlen() compute string length
strcpy() copy string to another
strcat() concatnets (join) two string
strcmp() compare two strings
strlwr() convert string to lower case
strupr() convert string to upper case
C string comparison program
We can create a function to compare two strings.
#include <stdio.h>
int compare_strings(char [], char []);
int main()
{
char a[1000], b[1000];
printf("Input
a string\n");
gets(a);
printf("Input
a string\n");
gets(b);
if (compare_strings(a, b) == 0)
printf("Equal strings.\n");
else
printf("Unequal strings.\n");
return 0;
}
int compare_strings(char a[], char b[])
{
int c = 0;
while (a[c] == b[c]) {
if (a[c] == '\0' || b[c] == '\0')
break;
c++;
}
if (a[c] == '\0' && b[c] == '\0')
return 0;
else
return -1;
}
(b)
Write
the C program to print the following pattern using
Nasted if loop?
Ans:- #include <stdio.h>
int main()
{
int i, j, n;
/* Input number of rows from user */
printf("Enter value of n: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
/* Print i number of stars */
for(j=1; j<=i; j++)
{
printf("*");
}
/* Move to next line */
printf("\n");
}
return 0;
}
(c) Draw flowchart to calculate X1+X2+X3…..+Xn ?
Ans:-
Q2. (a)
write the following function prototype with sample C code.
To find the maximum numbers among
two numbers?
(i)
with
argument ,no return type
(ii)
with
argument, with return type
1.
Ans:-
1.
Function with arguments but no return
value : When a function has arguments, it receive any
data from the calling function but it returns no values.
Syntax :
Function declaration : void function (int );
Function call : function( x );
Function definition:
void function( int x )
{
statements;
}
// C code for function // with argument but no return value #include <stdio.h> void function(int, int[], char[]); int main() { int a = 20; int ar[5] = { 10, 20, 30, 40, 50 }; char str[30] = "geeksforgeeks"; function(a, &ar[0],
&str[0]); return 0; } void function(int a, int* ar, char* str) { int i; printf("value of a
is %d\n\n", a); for (i = 0; i < 5; i++) { printf("value
of ar[%d] is %d\n", i, ar[i]); } printf("\nvalue of
str is %s\n", str); } |
Output:
value
of a is 20
value
of ar[0] is 10
value
of ar[1] is 20
value
of ar[2] is 30
value
of ar[3] is 40
value
of ar[4] is 50
The
given string is : geeksforgeeks
2. Function with arguments and return value
Syntax :
Function
declaration : int function ( int );
Function
call : function( x );
Function
definition:
int function( int x )
{
statements;
return x;
}
// C code for function with arguments // and with return value
#include <stdio.h> #include <string.h> int function(int, int[]); int main() { int i, a = 20; int arr[5] = { 10, 20, 30, 40, 50 }; a = function(a,
&arr[0]); printf("value of a
is %d\n", a); for (i = 0; i < 5; i++) { printf("value
of arr[%d] is %d\n", i, arr[i]); } return 0; } int function(int a, int* arr) { int i; a = a + 20; arr[0] = arr[0] + 50; arr[1] = arr[1] + 50; arr[2] = arr[2] + 50; arr[3] = arr[3] + 50; arr[4] = arr[4] + 50; return a; } |
Output:
value of a is 40
value of arr[0] is 60
value of arr[1] is 70
value of arr[2] is 80
value of arr[3] is 90
value of arr[4] is 100
(b)
write the C program to perform the matrix multiplication of two matrix of size
m x n and p x q respectively.
Ans:-
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Or
(b) write the c program to find the smallest number in
given 1D
Integer array by passing entire
array to finction”small”
(use prototype = with argument ,no
return type)
Ans:-
#include <iostream>
using namespace std;
int findSmallestElement(int arr[], int n){
/* We are assigning the first array element to
* the temp variable and then we are comparing
* all the array elements with the temp inside
* loop and if the element is smaller than temp
* then the temp value is replaced by that. This
* way we always have the smallest value in temp.
* Finally we are returning temp.
*/
int temp = arr[0];
for(int i=0; i<n; i++) {
if(temp>arr[i]) {
temp=arr[i];
}
}
return temp;
}
int main() {
int n;
cout<<"Enter the size of array: ";
cin>>n; int arr[n-1];
cout<<"Enter array elements: ";
for(int i=0; i<n; i++){
cin>>arr[i];
}
int smallest = findSmallestElement(arr, n);
cout<<"Smallest Element is: "<<smallest;
return 0;
}
Q4. (a) list the different operator in C. illustrate the
use logical
Operator to find the weather the entered character is upper
Case
of lower case or special symbol?
Ans:-
- Arithmetic
operators
- Relational
operators
- Logical
operators
- Bitwise
operators
- Assignment
operators
- Conditional
operators
- Special
operators
Program:-
#include<stdio.h>
int main()
{
//Fill the code
char ch;
scanf(“%c”,&ch);
if(ch
>= 65 && ch <= 90)
printf(“Upper”);
else if(ch
>= 97 && ch <= 122)
printf(“Lower”);
else if(ch
>= 48 && ch <= 57)
printf(“Number”);
else
printf(“Symbol”);
return 0;
}
OR
(a) Illustrate
the following with sutaible example
i)
How much total memory space allocated in 2D
array
ii)
How the element of 2D array are stored in
memory
Ans:-
(i)
Array
indexes always start from 0. Hence the first element of the 2D array is at
intArr[0][0]. This is the first row-first column element. Since it is an
integer array, it occupies 4 bytes of space. Next memory space is occupied by
the second element of the first row, i.e.; intArr [0][1] – first row-second
column element. This continues till all the first row elements are occupied in
the memory. Next it picks the second row elements and is placed in the same way
as first row. This goes on till all the elements of the array are occupies the
memory like below. This is how it is placed in the memory. But seeing the
memory address or the value stored in the memory we cannot predict which is the
first row or second row or so.
Total
size/ memory occupied by 2D array is calculated as
Total memory allocated to 2D Array = Number of elements * size of
one element
= Number of Rows *
Number of Columns * Size of one element
Total memory allocated to an Integer Array of size MXN = Number
of elements * size of one element
=M Rows* N Columns * 4 Bytes
= 10*10 * 4 bytes = 400 Bytes,
where M =N = 10
= 500*5 *4 bytes= 10000 Bytes,
where M=500 and N= 5
Total memory allocated to an character Array of N elements=
Number of elements * size of one element
= M Rows* N Columns * 1 Byte
= 10*10 * 1 Byte = 100 Bytes,
where N = 10
= 500*5 * 1 Byte = 2500 Bytes,
where M=500 and N= 5
(ii)
Let a be a
two dimensional m x n array.
Though a is pictured
as a rectangular pattern with m rows
and n columns, it is
represented in memory by a block of m*n sequential memory locations.
However
the sequence can be stored in two different ways:
- Column Major Order
- Row Major Order
Column Major Order
In the
column major order, the elements are stored column by column. First column,
second column and so on
For int nuber[3][12]; column major
order would look like:
Row Major Order
In row
majaor order the elements area stored row by row. First row, second row and so
on.
For int number[3][2]; Row
major order will look like:
Like
linear array, system keeps track of the address of first element only i.e. the base address all of the array.
Using this
base address, the computer computes the address of the element in the ith row and jth column,
i.e. loc a[i][j],
using the formulae:
Column Major Order:
loc
a[i][j]-base(a) + w(m Ñ… j + i)
Row
Major Order:
loc
a[i][j] base (a) + w (n x i +j)
where base(a) is base address of
array a, m is number of rows in array a, n is number of column in array, w is the number of bytes per
storage location for
one element of the array.
(b)write the C program to entered number is prime number
or not?
Ans:-
#include <stdio.h>
int main() {
int n, i, flag =
0;
printf(
"Enter a positive integer: ");
scanf(
"%d", &n);
for (i =
2; i <= n /
2; ++i) {
// condition for non-prime
if (n % i ==
0) {
flag =
1;
break;
}
}
if (n ==
1) {
printf(
"1 is neither prime nor composite.");
}
else {
if (flag ==
0)
printf(
"%d is a prime number.", n);
else
printf(
"%d is not a prime number.", n);
}
return
0;
}
Q5.(a) write C program to
to print the number of vowels and
Consonant in the given string using string pointer?
Ans:-
#include<stdio.h>
void main()
{
char
str[200];
int
i,vowels=0,consonants=0,
printf("Enter a string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||str[i]=='o' ||
str[i]=='u' || str[i]=='A' ||str[i]=='E' || str[i]=='I' || str[i]=='O'
||str[i]=='U')
{
vowels++;
}
else
if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&&
str[i]<='Z'))
{
consonants++;
}
}
printf("\nVowels = %d",vowels);
printf("\nConsonants = %d",consonants);
return 0;
}
OR
(a) Write
the C program to print the given lower
case string into uppercase string?
ans:-
#include<stdio.h>
#include<string.h>
int main(){
/* This array can hold a string of upto 25
* chars, if you are going to enter larger string
* then increase the array size accordingly
*/
char str[25];
int i;
printf("Enter the string: ");
scanf("%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nLower Case String is: %s",str);
return 0;
}
(b) Swap two numbers using user defined
“swap” function using -call by
Reference?
Ans:-
#include<stdio.h>
int main()
{
int a,b;
printf("enter the values of a
and b \n");
scanf("%d%d",&a,&b);
swap(&a,&b);
}
int swap(int*m,int*n)
{
int t;
t=*m;
*m=*n;
*n=t;
printf("after swapping,values
of \na=%d \nb=%d",*m,*n);
return 0;
}
(c) (i)
What is pointer ? list the different advantages of using pointer
.explain how to declare and initialize pointer variable
With syntax and example. write a C
program to print the value of integer variable using pointer
Ans:-
(i)
What is Pointer
in C?
The Pointer in
C, is a variable that stores address of another variable. A pointer can also be
used to refer to another pointer function. A pointer can be
incremented/decremented, i.e., to point to the next/ previous memory location.
The purpose of pointer is to save memory space and achieve faster execution
time.
ADVANTAGE:-
(i) Pointers make the programs simple and reduce their length.
(ii) Pointers are helpful in allocation and de-allocation of memory during the
execution of the program. Thus, pointers are the instruments of dynamic memory management.
(iii) Pointers enhance the execution speed of a program.
(iv) Pointers are helpful in traversing through arrays and
character strings. The strings are also arrays of characters terminated by the
null character (‘\O’).
(v) Pointers also act as references to different types of objects
such as variables, arrays, functions, structures, etc. However, C language does
not have the concept of references as in C++. Therefore, in C we use pointer as
a reference.
(vi) Storage of strings through pointers saves memory space.
Declaration of C Pointer variable
The general syntax of pointer
declaration is,
datatype
*pointer_name
;
Initialization of C Pointer variable
Pointer Initialization is
the process of assigning address of a variable to a pointer variable.
It contains the address of a variable of the same data type. In C language address operator &
is used to determine the address of
a variable. The &
(immediately
preceding a variable name) returns the address of the variable associated with
it.
inta
=10;
int*
ptr
;//pointer declaration
ptr
=&
a
;//pointer initialization
C PROGRAM
#include<stdio.h>
int main()
{
int num;
printf("Enter any number to store in \"num\" variable: ");
scanf("%d", &num);
printf("\nValue of num = %d", num);
printf("\nAddress of num = %u", &num);
getch();
return 0;
}
Q6. (a)
(i)
define structure. explain with syntax and example how to define
and declare and initialize the structure at compile time and run time
(ii)
illustrate the use of dot (.) and arrow(>) operator to
accesses element of structure with suitable C code
Ans:- structure is a user defined data
type in C/C++. A structure creates a
data type that can be used to group items of possibly different types into a
single type.
Declaring
structure variable using struct keyword.
Syntax
struct name variables;
Example
struct car
{
char name[100];
float price;
};
struct car car1, car2, car3;
Example
int i;
float f;
In structure, data type is <struct name>. So, the declaration
will be
<struct name> variables;
Example
struct car car1;
Initializing
structure members
We can initialize the structrue members directly like below,
Example
struct car
{
char name[100];
float price;
};
//car1
name as "xyz"
//price
as 987432.50
struct car car1 ={"xyz", 987432.50};
How to declare a structure?
We use struct keyword to declare a structure.
Let us declare a student structure containing three fields
i.e. name, roll and marks.
struct student
{
char
name
[100];
int
roll
;
float
marks
;
};
Initialize
structure using dot operator
In C, we initialize or access a structure variable either
through dot . or arrow -> operator. This is the most easiest way to initialize or access a
structure.
Example:
// Declare structure variable
structstudent stu1
;
// Initialize structure members
stu1
.name
="Pankaj";
stu1
.roll
=12;
stu1
.marks
=79.5f;
(ii)
The (.) dot operator
To assign the value "zara" to the first_name member
of object emp, you would write something as follows −
strcpy(emp.first_name, "zara");
The (->) arrow operator
If p_emp is a pointer to an object of type Employee, then to
assign the value "zara" to the first_name member of
object emp, you would write something as follows −
strcpy(p_emp->first_name, "zara");
#include <stdio.h>
/* structure declaration */
typedef struct student {
char srollno[10];
char sclass[10];
char name[25];
char fname[25];
char mname[25];
char add[200];
}Student;
void dot_access(Student const); /* prototype */
int main(void)
{
Student a = {"35M2K14", "cs", "Christine", "James", "Hayek",
"Post Box 1234, Park Avenue, UK"};
printf("Student a Information:\n");
dot_access(a); /* entire 'a' is passed */
return 0;
}
/* entire Student 'a' is copied into Student 'stu' */
/* 'stu' is a variable of Student, not a pointer */
void dot_access(Student const stu)
{
/* Let's access members of 'a' using dot operator */
print("roll no.: %s\n", stu.srollno);
printf("class: %s\n", stu.sclass);
printf("name: %s\n", stu.name);
printf("father's name: %s\n", stu.fname);
printf("mother's name: %s\n", stu.mname);
printf("And address: %s\n", stu.add);
}
(b) write
a C program to
-
define the structure “employee”
with element
emp_id , emp_name , emp_salary
-
declare the structure for 10 employee and initialize at run time
-
display the information of employee those the salary is >=
50000, by passing entire structure to user define function “display” using
pointer
Ans:-
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
int salary;
} Employee;
int main()
{
int i, n=2;
Employee employees[n];
//Taking each employee detail as input
printf("Enter %d Employee Details \n \n",n);
for(i=0; i<n; i++){
printf("Employee %d:- \n",i+1);
//Name
printf("Name: ");
scanf("%s",employees[i].name);
//ID
printf("Id: ");
scanf("%d",&employees[i].id);
//Salary
printf("Salary: ");
scanf("%d",&employees[i].salary);
printf("\n");
}
//Displaying Employee details
printf("-------------- All Employees Details ---------------\n");
for(i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%d \n",employees[i].salary);
printf("\n");
}
return 0;
}
download this file link
0 Comments