Contents

  1. .
  2. ./array_walk.c
  3. ./cmd-args.c
  4. ./dotproduct.c
  5. ./index.html
  6. ./pointers-functions.c
  7. ./sizeof_example.c

. 1/7

[
top][prev][next]


./array_walk.c 2/7

[
top][prev][next]
/*
 * Demonstrates walking through an array, using pointer arithmetic.
 * Adds up the numbers input, until we hit 0 or run out of input.
 */
 
#include <stdio.h>
 
int main() {
    int iarray[100];
    int n, num, status, sum, i;
    int* iptr;
    
    /* read in numbers, stopping when a 0 is read. */
    iptr = iarray;
    n=0;
    
    while( n < 100 ) {
        status = scanf("%d", &num);
        if( status == 0 || num == 0 ) {
            break;
        }
        *iptr++ = num;
        n++;
    }
    
    /* add the numbers */
    for( iptr = iarray, sum=0; n > 0; n--) {
        sum += *iptr++;
    }
    
    printf("sum = %d\n", sum);
}

./cmd-args.c 3/7

[
top][prev][next]
/* Print out the command line arguments
 * they are an array of strings*/

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    int i,j;
    
    printf("argc: %d\n", argc);
    
    printf("Iterating through args char by char\n");
    for (i=0; i<argc; i++) {
        j=0;
        while (argv[i][j] != '\0') {
            printf("%c",argv[i][j]);
            j++;
        }
        printf("\n");
    }
    
    printf("Iterating through arguments as strings \n");
    for (i=0; i<argc; i++)
        printf("%s\n",argv[i]);
}


./dotproduct.c 4/7

[
top][prev][next]
/*
 * Read in an integer N, then two vectors of N ints each.
 * Print out the dot product of the two vectors.
 * Demonstrates use of dynamic memory allocation.
 */
 
#include <stdio.h>
#include <stdlib.h>

void readVec(int size, int vec[]);

// computes the dot product of two integer vectors, 
// each of size size
int dotprod(int *vec1, int *vec2, int size) {
    int i, dp;
    for(i=0, dp=0; i < size; i++ ) {
        dp += vec1[i] * vec2[i];   
    }
    return dp;
}

void readVec(int size, int vec[]) {
    int i, num, status;
    
    for( i=0; i < size; i++ ) {
        status = scanf("%d", &num);
        if( status == 0 || num == 0 ) {
            break;
        }
        vec[i] = num;
    }
}

int main() {
    int *vec1, *vec2, size;
    scanf("%d", &size); // read in the size of the vectors -- should check for errors.
    
    // allocate space for the vectors
    vec1 = malloc(size*sizeof(int));
    vec2 = malloc(size*sizeof(int));
    
    if( vec1 == NULL || vec2 == NULL ) { // error check
        fprintf(stderr, "Out of memory!\n");
        return 1;
    }
    
    // read in the vectors
    readVec(size, vec1);
    readVec(size, vec2);
    
    // compute and print the dot product
    printf("dot product = %d\n", dotprod(vec1, vec2, size));
}

./index.html 5/7

[
top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/04-c</title>
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/assignments/assign.css" />
</head>
<body>
<h1>Examples for /home/faculty/sprenkle/public_html/cs330/examples/04-c</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//array_walk.c">array_walk.c</a></li>
<li><a href=".//cmd-args.c">cmd-args.c</a></li>
<li><a href=".//dotproduct.c">dotproduct.c</a></li>
<li><a href=".//pointers-functions.c">pointers-functions.c</a></li>
<li><a href=".//sizeof_example.c">sizeof_example.c</a></li>
</ul>
</body>

./pointers-functions.c 6/7

[
top][prev][next]
/*
 * Demonstrate returning value from function call via pointer.
 */

#include <stdio.h>

int division(int numerator, int denominator, int* dividend, int* remainder) {
	printf("address stored in dividend: %u\n",dividend);
	printf("address stored in remainder: %u\n",remainder);
	if (denominator < 1)
		return 0;
	*dividend=numerator/denominator;
	*remainder=numerator%denominator;
	numerator=7;
}

int main(int argc, char *argv[]) {
	int	x,y,d,r;
	
	x=9;
	y=2;
	
	printf("address of d: %u\n",&d);
	printf("address of r: %u\n",&r);
	
	division(x,y,&d,&r);
	
	printf("%d/%d = %d with %d remainder\n",x,y,d,r);
	printf("x=%d\n",x);
}

./sizeof_example.c 7/7

[
top][prev][next]
/*
 * Example using sizeof.
 * Demonstrates importance of knowing what you're passing to a function.
 */

#include<stdio.h>

int function(int x[]) {
    return (int) sizeof(x);   
}

int main() {
    int a[20];
    printf("sizeof(int) = %d\n sizeof(a) = %d\n", sizeof(int), sizeof(a));
    printf("sizeof(&a) = %d\n", sizeof(&a));

    printf("function returns %d\n", function(a));
}

Generated by GNU Enscript 1.6.6.