Software Engineering 14
Programming in C
Class Syllabus #4
Bubble Sort, String reading, ctype.h,strlen(),strcmp(), ? : operator, the need for functions, #ifdef #endif
/*bubleSort.c*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
//#define DEBUG
#define MAX_NUMBERS 1000
#define MAX_BUFFER 256
int main()
{
double data[MAX_NUMBERS];//this array is the actual data
int n=0;//this is the number of data elements
char inString[MAX_BUFFER];//used to input numbers
int dotPos;//signifies the position of the dot in the number (###.####), -1 if no dot
double pow; //helps convert string to float
int i,j;//iterators
char numFlag=1;//signifies when to leave input loop
double temp;//for switch in buble sort
printf("ENTER REAL POSITIVE NUMBERS.\n"
"(TO STOP ENTERING THE NUMBERS ENTER A NON-DIGIT CHARACTER)\n");
while(numFlag & n<=MAX_NUMBERS)//this is the input loop
{
/*Some think it's nice to write:1'st
2'nd
3'rd
4'th 5'th 6'th...... */
printf("Enter the %d'%s number: ",n+1,
1==n+1 ? "st" : (2==n+1 ? "nd" : (3==n+1 ? "rd" : "th")) );
scanf("%s",inString);
#ifdef DEBUG
printf("***%s***\n",inString);
#endif
for(dotPos=0; inString[dotPos]!='.' & inString[dotPos]; dotPos++) /**/;
//above loop finds dotPos if exists
data[n]=0.00;//start of with 0
for(pow=1, i=dotPos-1; i>=0 ; i--,pow*=10)
if(isdigit(inString[i]) )
data[n]+=pow * (inString[i]-'0');
else
{
numFlag=0;//if it was a non-digit number time to quit
break;
}
for(pow=0.1, i=dotPos+1; i<(signed int)strlen(inString);i++, pow /=10)
if(isdigit(inString[i]) )
data[n]+=pow * (inString[i]-'0');
else
{
numFlag=0;//if it was a non-digit number time to quit
break;
}
if(strcmp(".",inString) == 0 )
numFlag=0;//handle extreme case of "." string
#ifdef DEBUG
printf("%lf\n",data[n]);
#endif
n++;
}n--;//decrement because last num did not count
if(!n)
exit(0);//nothing to sort and print
#ifdef DEBUG
printf("These are the numbers entered:\n");
for(i=0;i<n;i++)
printf("%lf\t",data[i]);
putchar('\n');
#endif
/*performs the bubble sort*/
for(i=0; i<n-1 ; i++) //loop on whole array
for(j=n-1;j>i;j--) //loop on the top i slots
if(data[j-1] > data[j])
{/*switch*/
temp=data[j-1];
data[j-1]=data[j];
data[j]=temp;
}
printf("Here is the sorted list:\n");
for(i=0; i<n; i++)
printf("%d:\t%g\n",i+1,data[i]);
return 0;//all ok
}//end of main
Multidimensional array operations, using logical expressions
/*matrix.c*/
#include <stdio.h>
#define HEIGHT 4
#define WIDTH 3
#define SMOOTHING "The matrix is smoothed by averaging each cell with it's neighbors:"
void main()
{
int row,col;
float matrix[HEIGHT][WIDTH];
float smoothMatrix[HEIGHT][WIDTH];
float fakePadding[WIDTH];//formally important to insure that smoothing will
//not cause overflow
for(row=0; row < HEIGHT; row++)//get matrix from user
{
printf("Enter %d real numbers for row #%d (separated by white space):",WIDTH,row+1);
for(col=0;col < WIDTH; col++)
scanf("%f",&matrix[row][col]);
fflush(stdin);//so that if user entered more than needed it will not disturb
}
printf(SMOOTHING"\n");
for(row=0;row<HEIGHT;row++)
for(col=0;col<WIDTH;col++)//loop on each element of matrix
{
smoothMatrix[row][col]=
(row!=0)* ((col!=0)*matrix[row-1][col-1]+matrix[row-1][col]+(col!=WIDTH-1)*matrix[row-1][col+1])+
((col!=0)*matrix[row][col-1] +matrix[row][col]+ (col!=WIDTH-1)*matrix[row][col+1])+
(row!=HEIGHT-1)*((col!=0)*matrix[row+1][col-1]+matrix[row+1][col]+(col!=WIDTH-1)*matrix[row+1][col+1]);
smoothMatrix[row][col]/=9 - 3*(0==row) -3*(HEIGHT-1 == row) -
3* (0==col)-3*(WIDTH-1==col) + (0==row & 0 ==col)
+ (0==row & WIDTH-1==col) + (HEIGHT-1==row & 0==col)
+ (HEIGHT-1 == row & WIDTH-1 == col);
}
for(row=0;row<HEIGHT;row++)//print the smoothed matrix
for(col=0;col<WIDTH;col++)
printf("%g%c",smoothMatrix[row][col],(col + 1) % 3 ? '\t' : '\n');
//every WIDTH collumn print \n instead of \t
}
Basic String operations
/*squeezeString.c*/
#include <stdio.h>
#include <string.h>
#define STRING_SIZE 0xffff //32K
void main()
{
int s,c,len=0;
char string[STRING_SIZE];
char compress[STRING_SIZE*2];//can expand twice at worst case
//loop untill room is out or end of file is reached
while( len<STRING_SIZE-1 & (string[len++]=getchar()) != EOF);
string[--len]='\0';//cap off the string where EOF was put (we loose last char if non EOF)
//now len signfies length of string
s=0;c=0;
while(string[s])//loop on string and squeeze it
{
compress[c]=string[s];
compress[c+1]=0;
do
compress[c+1]++;
while(string[++s]==compress[c]);
c+=2;
}
compress[--c]='\0';//cap compress ( c is now the compressed size)
printf("Original size: %d\n"
"Compressed size: %d\n",len,c);
printf("This is a printout, taken from the compressed string:\n");
for(s=0;compress[s-1];s+=2)
for(c=0;c<compress[s+1];c++)
putchar(compress[s]);
}
Output
D:\Yoni\C_course\squeezeString\Debug>sq.exe < good.txt
Original size: 209
Compressed size: 75
:This is a printout, taken from the compressed string
a
aa
aaa
aaaa
bbbbb
cccccc
ddddddd
eeeeeeee
fffffffff
gggggggggg
hhhhhhhhhhh
iiiiiiiiiiii
jjjjjjjjjjjjj
kkkkkkkkkkkkkk
lllllllllllllll
mmmmmmmmmmmmmmmm
nnnnnnnnnnnnnnnnn
oooooooooooooooooo
ppppppppppppppppppp
>D:\Yoni\C_course\squeezeString\Debug
1