/* hello.c -- The most famous program of them all ..
*/
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
// return 0;
}
/* power2.c -- Print out powers of 2: 1, 2, 4, 8, .. up to 2^N
*/
#include <stdio.h>
#define N 16
int main(void) {
int n; /* The current exponent */
int val = 1; /* The current power of 2 */
printf("\t n \t 2^n\n");
printf("\t================\n");
for (n=0; n<=N; n++) {
printf("\t%3d \t %6d\n", n, val);
val = 2*val;
}
return 0;
}
/* It prints out :
n 2^n
================
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
*/
/* homework1.c -- This is how the code for the first homework
* appears when we have a single block letter.
* In our Unix system you can compile, link,
* load, and run this program with the commands
* % cc homework1.c
* % a.out
*/
#include <stdio.h>
void blockg(void); /*Prototype for blockg function */
int main (void) {
printf("\n");
blockg();
printf("\n");
}
/* Print out the Block letter g */
void blockg(void) {
printf("gggggg\n");
printf("g g\n");
printf("g\n");
printf("g ggg\n");
printf("g g\n");
printf("gggggg\n");
}
/* It prints out:
gggggg
g g
g
g ggg
g g
gggggg
*/
/* add2.c -- Add two numbers and print them out together
with their sum
AUTHOR:
DATE:
*/
#include <stdio.h>
int main(void) {
int first, second;
printf("Enter two integers > ");
scanf("%d %d", &first, &second);
printf("The two numbers are: %d %d\n", first, second);
printf("Their sum is %d\n", first+second);
}
/* addn.c -- Read a positive number N. Then read N integers and
* print them out together with their sum.
*/
#include <stdio.h>
int main(void) {
int n; /* The number of numbers to be read */
int sum; /* The sum of numbers already read */
int current; /* The number just read */
int lcv; /* Loop control variable, it counts the number
of numbers already read */
printf("Enter a positive number n > ");
scanf("%d",&n); /* We should check that n is really positive*/
sum = 0;
for (lcv=0; lcv < n; lcv++) {
printf("\nEnter an integer > ");
scanf("%d",¤t);
/* printf("\nThe number was %d\n", current); */
sum = sum + current;
}
printf("The sum is %d\n", sum);
return 0;
}
/* add.c -- Read a sequence of positive integers and print them
* out together with their sum. Use a Sentinel value
* (say 0) to determine when the sequence has terminated.
*/
#include <stdio.h>
#define SENTINEL 0
int main(void) {
int sum = 0; /* The sum of numbers already read */
int current; /* The number just read */
do {
printf("\nEnter an integer > ");
scanf("%d", ¤t);
if (current > SENTINEL)
sum = sum + current;
} while (current > SENTINEL);
printf("\nThe sum is %d\n", sum);
}
/* FILE: coins.c
* DETERMINES THE VALUE OF A COIN COLLECTION
* A Variation of the Hanly/Koffman book's example
*/
#include <stdio.h>
void main ()
{
// Local data ...
int pennies; // input: count of pennies
int nickels; // input: count of nickels
int dimes; // input: count of dimes
int quarters; // input: count of quarters
int temp, left; // temporaries for various
// computations
// Read in the count of quarters, dimes, nickels and pennies.
printf("Enter the number of quarters, dimes, nickels, and pennies: ");
scanf("%d %d %d %d", &quarters, &dimes, &nickels, &pennies);
// Compute the total value in cents.
left = 25 * quarters + 10 * dimes + 5 * nickels + pennies;
// Find and display the value in dollars
printf("Your collection is worth\n ");
temp = left / 100;
printf("\t%d dollar", temp);
if (temp==1)
printf(", ");
else
printf("s, ");
left = left % 100;
// Find and display the value left in quarters
temp = left / 25;
printf("%d quarter", temp);
if (temp==1)
printf(", ");
else
printf("s, ");
left = left % 25;
// Find and display the value left in dimes
temp = left / 10;
printf("%d dime", temp);
// Here, just for fun, instead of using a conditional statement,
// I use a conditional expression and string concatenation
printf ((temp==1) ? ", " : "s, ");
left = left % 10;
// Find and display the value left in nickels
temp = left / 5;
printf("%d nickel", temp);
if (temp==1)
printf(", and ");
else
printf("s, and ");
left = left % 5;
// Find and display the value left in pennies
printf("%d penn", left);
if (left==1)
printf("y\n");
else
printf("ies\n");
}
/* factorial.c -- It computes repeatedly the factorial of an integer entered
* by the user. It terminates when the integer entered is not
* positive.
*/
#include <stdio.h>
int fact(int n);
int main(void) {
int current;
printf("Enter a positive integer [to terminate enter non-positive] > ");
scanf("%d", ¤t);
while (current > 0) {
printf("The factorial of %d is %d\n", current, fact(current));
printf("Enter a positive integer [to terminate enter non-positive] > ");
scanf("%d", ¤t);
}
}
/* n is a positive integer. The function returns its factorial */
int fact(int n) {
int lcv; /* loop control variable */
int p; /* set to the product of the first lcv positive integers */
for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++);
return p;
}
/* prime1.c It prompts the user to enter an integer N. It prints out
* if it is a prime or not. If not, it prints out a factor of N.
*/
#include <stdio.h>
int main(void) {
int n;
int i;
int flag;
printf("Enter value of N > ");
scanf("%d", &n);
flag = 1;
for (i=2; (i<(n/2)) && flag; ) { /* May be we do not need to test
values of i greater than the square root of n? */
if ((n % i) == 0) /* If true n is divisible by i */
flag = 0;
else
i++;
}
if (flag)
printf("%d is prime\n", n);
else
printf("%d has %d as a factor\n", n, i);
return 0;
}
/* factor1.c -- It prompts the user to enter an integer N. It prints out
* if it is a prime or not. If not, it prints out all of its
* proper factors.
*/
#include <stdio.h>
int main(void) {
int n,
lcv,
flag; /* flag initially is 1 and becomes 0 if we determine that n
is not a prime */
printf("Enter value of N > ");
scanf("%d", &n);
for (lcv=2, flag=1; lcv <= (n / 2); lcv++) {
if ((n % lcv) == 0) {
if (flag)
printf("The non-trivial factors of %d are: \n", n);
flag = 0;
printf("\t%d\n", lcv);
}
}
if (flag)
printf("%d is prime\n", n);
}
/* true.c -- What are in C the values of TRUE and FALSE?
*/
#include <stdio.h>
int main(void) {
printf("The value of 1<2 is %d\n", (1<2));
printf("The value of 2<1 is %d\n", (2<1));
}
/* The program prints out
The value of 1<2 is 1
The value of 2<1 is 0
*/
/* fibo.c -- It prints out the first N Fibonacci
* numbers.
*/
#include <stdio.h>
int main(void) {
int n; /* The number of fibonacci numbers we will print */
int i; /* The index of fibonacci number to be printed next */
int current; /* The value of the (i)th fibonacci number */
int next; /* The value of the (i+1)th fibonacci number */
int twoaway; /* The value of the (i+2)th fibonacci number */
printf("How many Fibonacci numbers do you want to compute? ");
scanf("%d", &n);
if (n<=0)
printf("The number should be positive.\n");
else {
printf("\n\n\tI \t Fibonacci(I) \n\t=====================\n");
next = current = 1;
for (i=1; i<=n; i++) {
printf("\t%d \t %d\n", i, current);
twoaway = current+next;
current = next;
next = twoaway;
}
}
}
/* The output from a run of this program was:
How many Fibonacci numbers do you want to compute? 9
I Fibonacci(I)
=====================
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
*/
/* funcs.c -- Examples of function declarations, definitions, and use
*/
#include <stdio.h>
/* Examples of declarations of functions */
void square1(void); /* Example of a function without input parameters
and without return value */
void square2(int i); /* Example of a function with one input parameter
and without return value */
int square3(void); /* Example of a function without input parameters
and with integer return value */
int square4(int i); /* Example of a function with one input parameter
and with integer return value */
int area(int b, int h); /* Example of a function with two input parameters
and with integer return value */
/* Main program: Using the various functions */
int main (void) {
square1(); /* Calling the square1 function */
square2(7); /* Calling the square2 function using 7 as actual
parameter corresponding to the formal parameter i */
printf("The value of square3() is %d\n", square3()); /* Ysing the square3
function */
printf("The value of square4(5) is %d\n", square4(5)); /* Using the square4
function with 5 as actual parameter corresponding to i */
printf("The value of area(3,7) is %d\n", area(3,7)); /* Using the area
function with 3, 7 as actual parameters corresponding
to b, h respectively */
}
/* Definitions of the functions */
/* Function that reads from standard input an integer and prints
it out together with its sum */
void square1(void){
int x;
printf("Please enter an integer > ");
scanf("%d", &x);
printf("The square of %d is %d\n", x, x*x);
}
/* Function that prints i together with its sum */
void square2(int i){
printf("The square of %d is %d\n", i, i*i);
}
/* Function that reads from standard input an integer and returns
its square */
int square3(void){
int x;
printf("Please enter an integer > ");
scanf("%d", &x);
return (x*x);
}
/* Function that returns the square of i */
int square4(int i){
return (i*i);
}
/* Function that returns the area of the rectangle with base b
and hight h */
int area(int b, int h){
return (b*h);
}
/* The output of this program is:
Please enter an integer > 3
The square of 3 is 9
The square of 7 is 49
Please enter an integer > 4
The value of square3() is 16
The value of square4(5) is 25
The value of area(3,7) is 21
*/
/* funcs.c -- More examples of functions
*/
#include <stdio.h>
int getint(void); /*It prompts user to enter an integer, which it returns*/
int getmax(int a, int b, int c); /*It returns value of largest of a, b, c*/
/* Main program: Using the various functions */
int main (void) {
int x, y, z;
x = getint();
y = getint();
z = getint();
printf("The largest of %d, %d, and %d is %d\n", x, y, z, getmax(x,y,z));
}
int getint(void) {
int a;
printf("Please enter an integer > ");
scanf("%d", &a);
return(a);
}
int getmax(int a, int b, int c){
int m = a;
if (m<b)
m = b;
if (m<c)
m = c;
return(m);
}
/* array2.c -- Read/writing/reversing integer arrays
*/
#include <stdio.h>
#define NMAX 10
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[NMAX];
int hmny;
hmny = getIntArray(x, NMAX, 0);
printf("The array was: \n");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf("after reverse it is:\n");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
int temp = *x;
*x = *y;
*y = temp;
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void reverseIntArray(int a[], int n)
/* It reverse the order of the first n elements of a */
{
int i;
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}
/* linear.c -- Read an integer array and then do linear searches.
*/
#include <stdio.h>
#define NMAX 10
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
int linear(int a[], int n, int who);
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
hmny = getIntArray(x, NMAX, 0);
printf("The array was: \n");
printIntArray(x,hmny);
printf("Now we do linear searches on this data\n");
do{
printf("Enter integer to search for [0 to terminate] : ");
scanf("%d", &who);
if(who==0)break;
where = linear(x,hmny,who);
if (where<0){
printf("Sorry, %d is not in the array\n",who);
}else
printf("%d is at position %d\n",who,where);
}while(1);
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
int linear(int a[], int n, int who)
/* Given the array a with n elements, searches for who.
* It returns its position if found, otherwise it returns
* -1.
*/
{
int lcv;
for (lcv=0;lcv<n;lcv++)
if(who == a[lcv])return lcv;
return (-1);
}
/* getline.c -- Testing a function that reads a line
* from input.
*/
#include <stdio.h>
#define MAXBUF 128
int getline(char line[], int nmax);
int main(void){
int len;
char buffer[MAXBUF];
while(1){
len = getline(buffer, MAXBUF);
if (len==0)break;
printf("len = %d, line = %s\n", len, buffer);
};
}
int getline(char line[], int nmax)
/* It prompts user and reads up to nmax
* characters into line. It returns number
* of characters read. ['\n' terminates the line]
*/
{
int len;
char c;
len = 0;
printf("Enter a string [CR to exit]: ");
while(((c=getchar())!='\n') && len<nmax-1)
line[len++]=c;
line[len]='\0';
return len;
}
/* cpfile.c -- Similar to Unix's cp command.
* This program will be called with two parameters,
* the names of two files. It copies the first to the second.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[]){
FILE *fin, *fout;
char c;
if (argc!=3){
printf("Usage: %s filein fileout\n", argv[0]);
exit(0);
}
if ((fin=fopen(argv[1],"r"))==NULL){
perror("fopen filein");
exit(0);
}
if ((fout=fopen(argv[2],"w"))==NULL){
perror("fopen fileout");
exit(0);
}
while ((c=getc(fin))!=EOF)
putc(c,fout);
fclose(fin);
fclose(fout);
return 0;
}
/* binary.c - Binary search using two methods. The first is more intuitive,
but it is
slower.
*/
#include <stdio.h>
#include <sys/time.h>
/* Given a sorted array with n elements, we search for who using binary
search.
We return a position where found, or -1 if not there
*/
int binary1(int n, int a[n], int who) {
int left = 0;
int right = n-1;
while (left <= right) {
int mid = left + (right-left)/2;
if (who < a[mid])
right = mid - 1;
else if (who > a[mid])
left = mid + 1;
else
return mid;
}
return -1;
}
/* Given a sorted array with n elements, we search for who using binary
search.
We return a position where found, or -1 if not there
*/
int binary2(int n, int a[n], int who) {
int p = n/2;
while (n > 0) {
n = n/2;
if (who < a[p]) {
p -= n;
} else if (who > a[p]) {
p += n;
} else
return p;
}
return -1;
}
/* Returns the difference in microseconds between before and after */
long timediff(struct timeval before, struct timeval after) {
long sec = after.tv_sec - before.tv_sec;
long microsec = after.tv_usec - before.tv_usec;
return 1000000*sec + microsec;
}
int main() {
int a[] = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33};
int n = sizeof(a)/sizeof(int);
int where;
struct timeval before;
struct timeval after;
int k;
int j;
gettimeofday(&before, NULL);
for (j = 0; j < 1000000; j++)
for (k = 0; k < 2*n+1; k++) {
where = binary1(n, a, k);
// printf("who = %d, \tvalue = %d\n", k, where);
}
gettimeofday(&after, NULL);
printf("before=[%ld,%ld], after=[%ld,%ld]\n", before.tv_sec,
before.tv_usec,
after.tv_sec, after.tv_usec);
printf("The difference is %ld\n", timediff(before, after));
printf("---------------------------------------------\n");
gettimeofday(&before, NULL);
for (j = 0; j < 1000000; j++)
for (k = 0; k < 2*n+1; k++) {
where = binary2(n, a, k);
// printf("who = %d, \tvalue = %d\n", k, where);
}
gettimeofday(&after, NULL);
printf("before=[%ld,%ld], after=[%ld,%ld]\n", before.tv_sec,
before.tv_usec,
after.tv_sec, after.tv_usec);
printf("The difference is %ld\n", timediff(before, after));
return 0;
}
/* selection.c -- Read an integer array, print it, then sort it and
* print it. Use the selection sort method.
*/
#include <stdio.h>
#define NMAX 10
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void selectionSort(int a[], int n);
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
hmny = getIntArray(x, NMAX, 0);
if (hmny==0)
printf("This is the empty array!\n");
else{
printf("The array was: \n");
printIntArray(x,hmny);
selectionSort(x,hmny);
printf("The sorted array is: \n");
printIntArray(x,hmny);
}
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void selectionSort(int a[], int n)
/* It sorts in non-decreasing order the first N positions of A. It uses
* the selection sort method.
*/
{
int lcv;
int rh; /*Elements in interval rh..n-1 are in their final position*/
int where; /*Position where we have current maximum*/
int temp; /*Used for swapping*/
for(rh=n-1;rh>0;rh--){
/*Find position of largest element in range 0..rh*/
where = 0;
for (lcv=1;lcv<=rh;lcv++)
if (a[lcv]>a[where])
where = lcv;
temp = a[where];
a[where] = a[rh];
a[rh] = temp;
}
}
/* bubble.c -- Read an integer array, print it, then sort it and
* print it. Use the bubble sort method.
*/
#include <stdio.h>
#define NMAX 10
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void bubbleSort(int a[], int n);
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
hmny = getIntArray(x, NMAX, 0);
if (hmny==0)
printf("This is the empty array!\n");
else{
printf("The array was: \n");
printIntArray(x,hmny);
bubbleSort(x,hmny);
printf("The sorted array is: \n");
printIntArray(x,hmny);
}
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void bubbleSort(int a[], int n)
/* It sorts in non-decreasing order the first N positions of A. It uses
* the bubble sort method.
*/
{
int lcv;
int limit = n-1;
int temp;
int lastChange;
while (limit) {
lastChange = 0;
for (lcv=0;lcv<limit;lcv++)
/* Notice that the values in positions LIMIT+1 .. N are in
* their final position, i.e. they are sorted right */
if (a[lcv]>a[lcv+1]) {
temp = a[lcv];
a[lcv] = a[lcv+1];
a[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}