EMU CMPE112CMSE112 Qs Part II
EMU CMPE112CMSE112 Qs Part II
Q1)
What is printed by this program? Answer in the box:
#include <stdio.h>
double g(double u)
{
return u < 0 ? u - 1 : u + 1;
} 11.4
void f(double *px)
{
*px += g(*px);
}
void main()
{
double y = 5.2;
f(&y);
printf("\n %4.1f" , y);
}
Q2)
What is printed by this program? Answer in the box:
#include <stdio.h>
main()
{ 4 3 2 1
int *ptr;
int array[4] = {1, 2, 3, 4};
for(ptr=array+3; ptr >= array; ptr--)
printf("%d ", *ptr);
}
Q3)
What is printed by this program? Answer in the box:
#include <stdio.h>
main()
{
char *p = “ABC”; B
printf("%c\n", *(p + *p – ‘B’ + 2));
}
Q4
What is printed by this program? Answer in the box:
#include <stdio.h>
void fun(int *p)
{
int i;
i = 1;
while( i < 10)
{
*p = i;
i *= 3; 8 1 9 7
p++;
}
}
main()
{
int a[] = {8, 2, 9, 4, 6, 5, 7};
int i;
fun(a+2);
for(i=0; i < 7; i += 2)
printf("%d ", a[i]);
}
Q5
Fill in the blanks (denoted by ____ ) in the following program:
#include <stdio.h>
main()
{
void SetValue( _int *__ );
int x;
scanf("%d", &x);
SetValue( _&x___ );
printf(“The new value of x is: %d”, x);
}
#include <stdio.h>
#include <string.h>
int main(){
char name[30], surname[30];
printf(“Enter Name and Surname \n”);
scanf(“%s%s”,name,surname);
if (strcmp(name,surname)>0) printf(“%s %s”, name , surname);
else printf(“%s %s “,surname, name);
return 0;}
Q7
(a) Fill in the blanks in the program given below which is used for sorting three numbers in ascending
(increasing) order. For example, when prompted if the user enters
7.5 9.6 5.5
the program should print
The numbers in ascending order are: 5.50 7.50 9.60
Note: Lines that contain blanks are indicated by a ➢ in the left margin.
/*
* A program for ordering three numbers
*/
#include <stdio.h>
int main(void) {
double num1, num2, num3; /* three numbers to put in order */
/* Gets test data */
printf("Enter three numbers separated by blanks> ");
scanf("%lf%lf%lf", &num1, &num2, &num3);
/* Orders the three numbers */
2➢ order(__&num1___, ___&num2__);
3➢ order(__&num1___, ___&num3__);
4➢ order(__&num2__, ____&num3_);
/* Displays results */
printf("The numbers in ascending order are: %.2f %.2f %.2f\n",
num1, num2, num3);
return 0;
}
/*
* Arranges arguments in ascending order
*/
5➢ void order(_double*__smp, _double*__lgp) {
double temp;
/* Compares values and swaps if necessary */
6➢ if (__*smp____ > ___*lgp___) {
7➢ ___temp____ = ___*smp____;
8➢ ___*smp____ = ___*lgp____;
9➢ ___*lgp____ = ___temp____;
}
}
(b) What is the output of the following program?
#include <stdio.h>
void double_trouble(int *p, int y);
void trouble(int *x, int *y);
int main(void) {
int x, y;
trouble(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
Output
x=23, y=5
Q8
Consider the following C programs. What is printed? Provide the exact form of the output
as it is specified by the printf( ) statement.
Output
#include <stdio.h> 1
void foo(int * ); 9
main() 6
{
int a [3]= { 1,2,3} ;
foo(a);
printf("%d\n" , a[0]);
printf("%d\n" , a[1]);
printf("%d\n" , a[2]);
}
++ b;
*b *=2;
}
Q9
What is the output of the following program?
#include<stdio.h>
#include<string.h>
#define MAX 20
19
int main() will meet
{
char s1[MAX]="If you pass CMPE112"; 12
char s2[MAX]="we will meet"; in
char *s3="next semester";
char *s4="in CMPE212 course"; 0
char s5[3]="";
printf("%d\n",strlen(s1));
printf("%s\n",strrchr(s2,'w'));
printf("%s\n",strncat(s5,s4+8,2));
printf("%s\n",strncpy(s5,s4,2));
printf("%d\n",strncmp(s4+8,s1+17,2));
//printf("%s-%s-%s-%s-%s\n",s1,s2,s3,s4,s5);
return 0;
}
Q10)
Write a C program for the following string of operation.
Read a string of data from the monitor and after calling split function , divide the given string into two parts as
first and last and returns back to the main program.
Split function searches ‘*’ character in the given string and copies all the characters before ‘* character into first
and copies all the remaining characters after ‘*’ into last .
Afterwards compare first and last alphabetically in the main program and display the result as follow.
First is greater than last
First is less than last
First is equal to last
Example
book*abacus →input data
alphabetically book is greater than abacus → output of your program
abdullah*adem →input data
alphabetically abdullah is less than adem → output of your program
deniz*deniz →input data
alphabetically deniz is equal to deniz → output of your program
If necessary you can use some of the below string functions
strlen(s1): Computes the length of the string s1, and returns the number of characters that precede ‘\0’.
strcat( s1 , s2): Concatenates a copy of string s2 onto the string s1;
strcpy(s1, s2: Copies a string from s2 to s1.
strcmp(s1,s2): Compares the string s2 with the string s1. Returns an integer less than, equal to, or greater than
depending on the result of the comparison.
Q11)
Trace and list the output of the program
#include<stdio.h>
int main()
{
int a=3,b=5;
fsp(&a,b);
a+=b;
b+=a;
printf("Result of A=%d and B=%d",a,b);
return 0;
TRACE OUTPUT
int main()
{
int array[3]={5,2,6};
int i,n=6;
fp(array[0],array,n);
for(i=0;i<n/2;i++)
printf(" %d \n",array[i]);
return 0;
}
TRACE OUTPUT
Q13)
The following function decides whether the (nxn) matrix is upper triangular or not by returning 1 if it is upper
triangular and zero otherwise.
(Note: a matrix is upper triangular if all elements below the diagonal are zero)
diagonal elements
Example:
4 5 3 4
0 7 0 6
0 0 5 3
0 0 0 7
Q14
Using the following initializations in a program
int y[]={1,2,3,4,5};
int *x= y;
Q15
What are the values of the following expressions ?
Q16
Given the following initializations:
b) *(pscores+2) ………25………..…..
d) (pscores[2] + 5) ………30……………
e) --*pscores ………87……………
Q17
Given the following initializations
char word1[13] = “introduction”;
char word2[14] = “toprogramming”;
char str1[9] = “computer”;
char str2[12] = “engineering”
Q18
Write a C code to do the following task: Read a series of characters from the
standard input and write them to the standard output with the characters reversed,
i.e., if the input is Ahmet then the output will be temhA.
int main(void)
{
char str[100], str_r[100];
int n,i,k;
for(n=0,i=0;str[i]!='\0';i++)
n++;
for(k=n-1,i=0;i<n;i++,k--)
str_r[i]=str[k];
str_r[i]='\0';
printf("\n%s",str_r);
return 0;
}