csc1101_tutorial05_06

advertisement
KING SAUD UNIVERSITY
COLLEGE OF APPLIED STUDIES AND COMMUNITY
SERVICE
CSC 1101
First Semester 1432-1433
Tutorial #5 & 6
Array:
Question1:Find the error(s) in each of the following statements:
a) Assume: char str[ 5 ];
scanf( "%s", str );
/* User types hello */
ANS: str needs a minimum length of 6; one element for each letter in
hello and an element for the terminating NULL character.
b) Assume: int a[ 3 ];
printf( "$d %d %d\n", a[ 1 ], a[ 2 ], a[ 3 ] );
ANS: printf( “%d %d %d\n”, a[ 0 ], a[ 1 ], a[ 2 ] );
c) double f[ 3 ] = { 1.1, 10.01, 100.001, 1000.0001 };
ANS: Too many variables defined.
double f[ 3 ] = { 1.1, 10.01, 100.01 };
Question2:Write single statements that perform each of the following single
subscripted array operations:
a) Initialize the 10 elements of integer array counts to zeros.
ANS:
int counts[10];
for ( i = 0; i <10; i++ )
counts[ i ] = 0;
b) Add 1 to each of the 15 elements of integer array bonus.
ANS:
for ( i = 0; i <= 14; i++ )
++bonus[ i ];
c) Read the 12 values of floating-point array
monthlyTemperatures from the keyboard.
ANS:
for ( i = 0; i <= 11; i++ ) {
printf( “Enter a temperature: ” );
scanf( “%f”, &monthlyTemperatures[ i ] );
}
d) Print the 5 values of integer array bestScores in one column
format.
ANS:
for ( i = 0; i <= 4; i++ ) {
printf( “%d\n”, bestScores[ i ] );
Question3:Write statements to accomplish each of the following:
a) Determine and print the smallest and largest values contained
in 99-element floating-point array w.
ANS:
smallest = largest = w[ 0 ];
for ( i = 1; i <99 ; i++ )
if ( w[ i ] < smallest )
smallest = w[ i ];
else if ( w[ i ] > largest )
largest = w[ i ];
b) Initialize each of the 5 elements of single-subscripted integer
array g to 8.
ANS:
for ( i = 0; i <5; i++ )
g[ i ] = 8;
Question4:What is the output?
#include <stdio.h>
int main() {
int a[6],b[3],k,j=0;
printf("Enter 6 integer values >");
for(k=0;k<=5;k=k+2) {
scanf("%d%d",&a[k],&b[j]);
j++;
}
for(k=0;k<=2;k++) {
for (j=0;j<=4;j=j+2) {
if(b[k]>=a[j]) {
a[j+1]=b[k];
b[k]=a[j];
break;
}
}}
j=0;
for(k=1;k<=6;k=k+2) {
printf("%d\t%d\n",a[k],b[j]);
j++;
}
return 0;}
input typed by the user:
43
16
35
39
14
47
47
39
16
14
35
43
String:
Question1 : trace the following program:
#include<stdio.h>
#include<string.h>
int main(void)
{char x[]="Dear Maha, Hope you fine.... ",
y[10]= "Sara ",z[10];
strncpy(z,x,9);
z[9]='\0';
strncat(y,x,4);
printf("%s\n", z);
printf("%s\n", y);
return(0);}
Dear Maha
Sara Dear
Question 3:Write a program that uses function strcmp to compare two words input by the
user. The program should state whether the first string is less than, equal to or greater
than the second string.
Sample run:
Enter two strings: Greg Dave
"Greg" is greater than "Dave"
Enter two strings: Bill Bill
"Bill" is equal to "Bill"
Enter two strings: Pete Tim
"Pete" is less than "Tim"
1 /* Q 3 Solution */
2 #include <stdio.h>
3 #include <string.h>
4
5 int main()
6 {
7
char string1[ 20 ]; /* first string input by user */
8
char string2[ 20 ]; /* second string input by user */
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int result;
/* result of comparing two strings */
printf( "Enter two strings: " );
scanf( "%s%s", string1, string2 ); /* read two strings */
result = strcmp( string1, string2 );
/* display appropriate message for result */
if ( result ==1 ) {
printf( "%s is greater than %s\n", string1, string2 );
} /* end if */
else if ( result == 0 ) {
printf( "%s is equal to %s\n", string1, string2 );
} /* end else if */
else {
printf( "%s is less than %s\n", string1, string2 );
} /* end else */
return 0; /* indicate successful termination */
} /* end main */
Question 3 : Write a program that does the following:
a. Prompts the user to enter his first name then his last name in to two strings
fname, lname;
b. Display a message welcoming the user mentioning his full name
c.
Then print the user's name in reverse order.
Please enter your first name: Heba
Please enter your last name: Kasem
Hello Heba Kasem !!!
Your name in reverse order is: mesaK abeH
Press any key to continue
(Hint: store the first and last name in one string call it fullname)
#include<stdio.h>
#include<string.h>
int main(void)
{
char fname[20],lname[20],fullname[40]="";
int i;
printf("Enter firstname:");
scanf("%s",fname);
printf("Enter last name:");
scanf("%s",lname);
strcat(fullname,fname);
strcat(fullname," ");
strcat(fullname,lname);
printf("Hello %s!!!\n",fullname);
printf("Your name in reverse order is: ");
for(i=strlen(fullname)-1;i>=0;i--)
printf("%c",fullname[i]);
printf("\n");
return (0);
}
Download