MS WORD

advertisement
Introduction to Unix and C – Handout #7
Sample solution to problem #2 in Assignment #4
Shows how bit-wise operators are used.
/* Assignment #4 - Problem #2 sample solution
Author: Chamara Gunaratne [June 03, 2003]
The function 'invert' inverts n number of bits that occur after
the pth position in x */
#include <stdio.h>
int invert(int, int, int); /* function definition */
main()
{
int temp;
/* call function 'invert' */
temp = invert(173, 4, 2);
printf("The return value of call #1 = %d \n", temp);
}
/* function to invert set of bits */
int invert(int x, int p, int n)
{
int mask = 0;
int i, j;
int pow;
int temp1, temp2;
/* build-up the mask by adding up the 2 power values of bits */
for (i = p; i > (p - n); i--)
{
pow = 1; /* 2 power 0 = 1 */
if (i > 0)
{
for (j = 1; j <= i; j++)
pow = pow * 2;
}
mask = mask + pow;
}
/* printf("mask = %d \n", mask); */
temp1 = x & mask; /* AND with mask to isolate bits */
temp2 = ~temp1;
/* invert the isolated bits */
temp2 = temp2 & mask; /* AND with mask to eliminate extra bits */
return (x - temp1 + temp2);
}
Program skeleton:
/* program description here */
#define ...
/* variables */
float score[MAX_NUM];
float mean;
float sum;
int
sum_score;
int
i;
/* main program */
main()
{
/* enter the test scores from the keyboard */
/* sum up compute the mean of the test scores */
/* output the list of test scores and mean value */
}
Actual program
/* Example program for demonstrating good program structure
Desc: Accepts test scores from the keyboard. The list of test scores
and
their mean are output to the screen.
*/
#include <stdio.h>
#define MAX_NUM 5
/* max number of test scores */
main()
{
float
float
float
int
int
score[MAX_NUM];
mean;
sum;
num_score;
i;
/*
/*
/*
/*
/*
array of test cores */
mean value */
sum of test scores */
number of test scores entered */
loop counter */
/* output a descriptive banner */
printf("This program computes the mean of test scores. The \n");
printf("maximum number of test scores is assumed to be %d. \n",
MAX_NUM);
printf("\n");
/* enter test scores until -ve value or array is full */
num_score = 0;
while (1)
{
printf("Enter test score #%2d [-1 to quit] : ",
(num_score + 1) );
scanf("%f", &score[num_score]);
if ((score[num_score] < 0) || (num_score == (MAX_NUM 1)))
break;
num_score++;
}
/* sum up the score values */
sum = 0;
for (i = 0; i < num_score; i++)
{
sum = sum + score[i];
}
/* compute the mean */
if (num_score > 0)
mean = sum / num_score;
else
mean = 0;
/* output the test scores and the mean value */
printf("\n\n");
for (i = 0; i < num_score; i++)
printf("\tTest score #%2d = %f \n", (i + 1), score[i] );
printf("\n\tMean value = %f \n", mean );
}
Unix
Example #1
This is a directory listing using the –la command parameters.
148 Sun Server:~/UnixAndC/Lect03: ls -la
total 214
drwxr-xr-x
4 pgunarat grad
512 May 19 2003 ./
drwxr-xr-x
4 pgunarat grad
512 May 19 14:01 ../
-rwxr-xr-x
1 pgunarat grad
23872 May 19 14:20 a.out*
-rw-r--r-1 pgunarat grad
116 May 19 10:38 ex1.c
-rw-r--r-1 pgunarat grad
253 May 19 12:09 ex2.c
-rw-r--r-1 pgunarat grad
240 May 19 12:18 ex3.c
-rw-r--r-1 pgunarat grad
88 May 19 13:16 ex4.c
-rwxr-xr-x
1 pgunarat grad
24120 May 19 2003 ex4.out*
-rw-r--r-1 pgunarat grad
192 May 19 13:48 ex5.c
-rwxr-xr-x
1 pgunarat grad
23876 May 19 2003 ex5.out*
-rw-r--r-1 pgunarat grad
164 May 19 13:55 ex6.c
-rwxr-xr-x
1 pgunarat grad
24084 May 19 2003 ex6.out*
-rw-r--r-1 pgunarat grad
99 May 19 12:46 test.txt
drwxr-xr-x
2 pgunarat grad
512 May 19 2003 test_dir1/
drwxr-xr-x
2 pgunarat grad
512 May 19 2003 test_dir2/
149 Sun Server:~/UnixAndC/Lect03:
Using ‘chmod’ to change file parameters:
207 Sun Server:~/UnixAndC/solutions: ls -la
total 60
drwxr-xr-x
2 pgunarat grad
512 Jun
drwxr-xr-x
9 pgunarat grad
512 Jun
-rwxr-xr-x
1 pgunarat grad
23948 Jun
1 23:25 ./
2 09:53 ../
2 00:26 a.out*
208 Sun Server:~/UnixAndC/solutions: chmod 700 a.out
209 Sun Server:~/UnixAndC/solutions: ls -la
total 60
drwxr-xr-x
2 pgunarat grad
512 Jun 1 23:25 ./
drwxr-xr-x
9 pgunarat grad
512 Jun 2 09:53 ../
-rwx-----1 pgunarat grad
23948 Jun 2 00:26 a.out*
210 Sun Server:~/UnixAndC/solutions: chmod g+rwx a.out
211 Sun Server:~/UnixAndC/solutions: ls -la
total 60
drwxr-xr-x
2 pgunarat grad
512 Jun 1 23:25 ./
drwxr-xr-x
9 pgunarat grad
512 Jun 2 09:53 ../
-rwxrwx--1 pgunarat grad
23948 Jun 2 00:26 a.out*
212 Sun Server:~/UnixAndC/solutions:
Download