#10 (02/21/2024)

Misc. notes

Sorting algorithm (bubble sort)

x[0] x[1] x[2] x[3] x[4] x[5]
45 67 12 34 25 39
x[0] x[1] x[2] x[3] x[4] x[5]
45 67 12 34 25 39
x[0] x[1] x[2] x[3] x[4] x[5]
45 12 67 34 25 39
x[0] x[1] x[2] x[3] x[4] x[5]
45 12 34 67 25 39
x[0] x[1] x[2] x[3] x[4] x[5]
45 12 34 25 67 39
x[0] x[1] x[2] x[3] x[4] x[5]
45 12 34 25 39 67
The largest element has bubbled to the top index of the array. In general, a bubble step is performed by the loop:
#define N 6
for (i=0; i < N-1; i++)
if (x[i] > x[i+1]) swap(&x[i], &x[i+1])
If this process is repeated again for (i=0; i< N-2; i++), then, the second largest element is located at the second right most place.
Repeat this for (i=0; i< N-3; i++), for (i=0; i< N-4; i++), for (i=0; i< N-5; i++)
#include <stdio.h>
#define N 6

void swap(float *a, float *b)
{
float tmp;
tmp=*a;
*a=*b;
*b=tmp;
}

int main()
{

float a[N]={45, 67, 12, 34, 25, 39};
int  i, j;

for (j=1; j < N; j++)
 {for (i=0; i< N-j; i++)
   if ( a[i]> a[i+1]) swap(&a[i], &a[i+1]);
  }

 for (i=0;i<N; i++) printf("%f ", a[i]);
   printf("\n");
 return 0;
}
Demo

String manipulation

#include <stdio.h>
int main()
{
float a=2.0;
printf("%f\n",a);
return 0;
}
#include <stdio.h>
int main()
{
char a='A';
printf("%c\n",a);
return 0;
}
#include <stdio.h>
int main()
{
float a[]={2.0, 3.0, 4.0, 5.0};
printf("%f\n",a[0]);
return 0;
}
#include <stdio.h>
int main()
{
char a[]="MAE2360";
printf("%c\n",a[0]);
return 0;
}
Use the %s format to represent the entire string instead of the %c format that represents only one character.
#include <stdio.h>
int main()
{
char *a;
a="MAE2360";
printf("%c\n",a[0]);
return 0;
}
#include <stdio.h>
int main()
{
char *a;
a="MAE2360";
printf("%s\n",a);
return 0;
}

"ABC" (double quotation) is for a string of characters and is an array (pointer).
'A' (single quotation) is for a single character !!

Consider this example:
#include <stdio.h>
int main()
{
  char s[3] = "ABC";

  printf("%s\n", s);
  return 0;
}
To read a string from the standard input (i.e. keyboard),
#include <stdio.h>
int main()
{
char str[100];
printf("Enter string = ");
scanf("%s", str);
printf("%s\n",str);
return 0;
}
Note that there is no "&" before str in the scanf function as "str" is already a pointer. The C compiler automatically adds "\0" after the end of the last character in the string.

String copy/compare/length

To copy a string to another string or to compare a string against another string, it is best to use functions, strcpy and strcmp, found in string.h (you can avoid all the hassles associated with pointer manipulation). Reference to C functions
#include <stdio.h>
#include <string.h>
int main()
{
    char c1[] = "ABCDE", c2[6];

    strcpy(c2,c1);
    printf("%s\n",c2);
    return 0;
}
The function, strcpy(c2,c1), copies the contents pointed by c1 to the address pointed by c2.
To compare two strings, use strcmp found in string.h.
#include <stdio.h>
#include <string.h>

int main()
{
    char s[100];

    printf("Enter \"MAE2360\" = ");
    scanf("%s", s);

    if (strcmp(s,"MAE2360") == 0) printf("MAE2360 was entered correctly.\n");
    
    else printf("Wrong. %s was entered.\n",s);
    return 0;
}
The strcmp function takes two strings as the arguments and returns 0 if the two strings match. Note that you can output " (double quotation mark) by "escaping" it using the backslash character.
To find the length of the string, you may want to use the strlen function.
#include <stdio.h>
#include <string.h>
int main()
{
char c[50];
printf("Enter string = ");
scanf("%s", c);
printf("You entered %s\n", c);
printf("Its length is %d\n", strlen(c));
return 0;
}

File handling (from DOS/Shell prompt)

I/O Redirection (Standard input/output redirection)

Notion Meaning
$ a.out > filename output to file
$ a.out >> filename append output to file
$ a.out < filename get input from file

$ gcc myprogram.c
$ a.out > result.dat
$ more result.dat


$ gcc myprogram.c
$ a.out < data.dat > result.dat
$ more result.dat


c:\dir > filelist.dat

File handling (from within a program)


#include <stdio.h>

int main()
{
	FILE *fp;
 	fp = fopen("filename","w");
   /*
      write something on fp 
   */
	fclose(fp);
        return 0;
}

The function, fopen, takes a (append), w (write) or r (read) as possible arguments.

#include <stdio.h>
int main()
{
	FILE *fp;
	fp=fopen("junk.dat","w");
	fprintf(fp,"Hello!\n");
	fclose(fp);
        return 0;
}


#include <stdio.h>
int main()
{
	FILE *fp; float a,b,c;
	fp=fopen("junk.dat","r");
	fscanf(fp,"%f %f %f", &a, &b, &c);

	printf("%f %f %f", a,b,c);

	fclose(fp);
        return 0;
}


#include <stdio.h>
int main()
{
	FILE *fp1, *fp2; 
        float a,b,c;
	fp1=fopen("junk1.dat","w");
        fp2=fopen("junk2.dat","w");
	fprintf(fp1,"This is the first file.\n");
	fprintf(fp2,"This is the second file.\n");

	fclose(fp1); fclose(fp2);
        return 0;
}

Command line parameters

$ gcc -lm add.c -o add
$ junk 
(interactive session)
$ gcc -lm add.c -o add
$ junk 3421 8756
(executes program to manipulate 3421 and 8756 and prints results)
int main(int argc, char *argv[])
The first argument, argc, is of integer type and is assigned the number of command line arguments including the command name itself. The second argument, argv, is a pointer to an array of strings and is assigned pointers to each command line argument. Note that argv[] is an array so that it can take multiple command line arguments. 2
#include <stdio.h>

int main(int argc, char *argv[])
{
  int i;

  printf("Number of parameters = %d\n", argc);

  for(i=0; i<argc; i++)
    printf("%d: %s\n", i, argv[i]);
  
  return 0;
}
atoi (ASCII to INTEGER) or atof (ASCII to FLOAT) found in the stdlib.h library.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
printf("%d\n", atoi(argv[1]));
return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i;
int b[100]; /* reserve ample space*/

for (i=1; i< argc; i++)
b[i]=atoi(argv[i]);

for (i=1; i< argc ; i++)
 printf("%d\n", b[i]);

return 0;
}


Footnotes:

1 -lm is needed when math.h is to be read by the preprocessor directive.
2 Actually, argv[] is a pointer to another pointer (an array of another array) as each element of argv[] is a string of characters.


File translated from TEX by TTH, version 4.03.
On 19 Feb 2024, 13:57.