csc1101_tutorial05_06

```KING SAUD UNIVERSITY
COLLEGE OF APPLIED STUDIES AND COMMUNITY
SERVICE
CSC 1101
First Semester 1432-1433
Tutorial #5 &amp; 6
Array:
Question1:Find the error(s) in each of the following statements:
a) Assume: char str[ 5 ];
scanf( &quot;%s&quot;, 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( &quot;\$d %d %d\n&quot;, 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 &lt;10; i++ )
counts[ i ] = 0;
b) Add 1 to each of the 15 elements of integer array bonus.
ANS:
for ( i = 0; i &lt;= 14; i++ )
++bonus[ i ];
c) Read the 12 values of floating-point array
monthlyTemperatures from the keyboard.
ANS:
for ( i = 0; i &lt;= 11; i++ ) {
printf( “Enter a temperature: ” );
scanf( “%f”, &amp;monthlyTemperatures[ i ] );
}
d) Print the 5 values of integer array bestScores in one column
format.
ANS:
for ( i = 0; i &lt;= 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 &lt;99 ; i++ )
if ( w[ i ] &lt; smallest )
smallest = w[ i ];
else if ( w[ i ] &gt; largest )
largest = w[ i ];
b) Initialize each of the 5 elements of single-subscripted integer
array g to 8.
ANS:
for ( i = 0; i &lt;5; i++ )
g[ i ] = 8;
Question4:What is the output?
#include &lt;stdio.h&gt;
int main() {
int a[6],b[3],k,j=0;
printf(&quot;Enter 6 integer values &gt;&quot;);
for(k=0;k&lt;=5;k=k+2) {
scanf(&quot;%d%d&quot;,&amp;a[k],&amp;b[j]);
j++;
}
for(k=0;k&lt;=2;k++) {
for (j=0;j&lt;=4;j=j+2) {
if(b[k]&gt;=a[j]) {
a[j+1]=b[k];
b[k]=a[j];
break;
}
}}
j=0;
for(k=1;k&lt;=6;k=k+2) {
printf(&quot;%d\t%d\n&quot;,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&lt;stdio.h&gt;
#include&lt;string.h&gt;
int main(void)
{char x[]=&quot;Dear Maha, Hope you fine.... &quot;,
y[10]= &quot;Sara &quot;,z[10];
strncpy(z,x,9);
z[9]='\0';
strncat(y,x,4);
printf(&quot;%s\n&quot;, z);
printf(&quot;%s\n&quot;, 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
&quot;Greg&quot; is greater than &quot;Dave&quot;
Enter two strings: Bill Bill
&quot;Bill&quot; is equal to &quot;Bill&quot;
Enter two strings: Pete Tim
&quot;Pete&quot; is less than &quot;Tim&quot;
1 /* Q 3 Solution */
2 #include &lt;stdio.h&gt;
3 #include &lt;string.h&gt;
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( &quot;Enter two strings: &quot; );
scanf( &quot;%s%s&quot;, string1, string2 ); /* read two strings */
result = strcmp( string1, string2 );
/* display appropriate message for result */
if ( result ==1 ) {
printf( &quot;%s is greater than %s\n&quot;, string1, string2 );
} /* end if */
else if ( result == 0 ) {
printf( &quot;%s is equal to %s\n&quot;, string1, string2 );
} /* end else if */
else {
printf( &quot;%s is less than %s\n&quot;, 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.
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&lt;stdio.h&gt;
#include&lt;string.h&gt;
int main(void)
{
char fname[20],lname[20],fullname[40]=&quot;&quot;;
int i;
printf(&quot;Enter firstname:&quot;);
scanf(&quot;%s&quot;,fname);
printf(&quot;Enter last name:&quot;);
scanf(&quot;%s&quot;,lname);
strcat(fullname,fname);
strcat(fullname,&quot; &quot;);
strcat(fullname,lname);
printf(&quot;Hello %s!!!\n&quot;,fullname);
printf(&quot;Your name in reverse order is: &quot;);
for(i=strlen(fullname)-1;i&gt;=0;i--)
printf(&quot;%c&quot;,fullname[i]);
printf(&quot;\n&quot;);
return (0);
}
```