CSCI 255 — Assembler-like C

In this lab you are going to modify a C program to remove all well-structured control structures and to make arrays look a bit like pointer. You probably ought to open the lecture pages on translating C control structures and using pointers in C as references.

Starting with C

Start up Netbeans and create a C/C++ application within the C/C++ category. Consult the Java to C lab if you need to jog your memory.

Replace the initial fill-in-the-blanks C program with a a main routine and a function called genHistogram. Your will only modify the genHistogram function in this assignment.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>

// Here's the prototype for genHistogram
float genHistogram(
    const int values[],         // promise not to change any elements
    int size,
    int histogram[11],
    int *lowCount,
    int *highCount) ;

int main(int argc, char** argv) {

    int size ;
    int *sample ;
    int histogram[10] ;
    int tooLow, tooHigh ;
    float justAverage ;
    srand(time(NULL)) ;
    size = 64 + rand() % 16 ;
    printf("sample size is %d\n", size) ;
    // This is how your create "dynamic" arrays in C
    // This used to be done a lot in CSCI 202
    sample = (int *)malloc(size * sizeof(int)) ;
    printf("samples located at %lX\n\n", (unsigned long)(void *)&sample) ;
    // Stores random numbers from -3 to 13
    for (int i=0; i<size; ++i) {
        sample[i] = rand()%5 + rand()%5 + rand()%5 + rand()%5 - 3 ;
    puts("Samples") ;
    for (int i=0; i<size; ++i) {
        printf("S[%2d] == %2d\n", i, sample[i]) ;
    justAverage = genHistogram(sample, size, histogram, &tooLow, &tooHigh) ;
    puts("") ;
    puts("Results") ;
    printf("Too low:  %2d\n", tooLow) ;
    printf("Too high: %2d\n", tooHigh) ;
    for (int i=0; i<11; ++i) {
        printf("H[%2d] == %2d\n", i, histogram[i]) ;
    printf("Average:  %4.2f", justAverage) ;
    return (EXIT_SUCCESS);

// This is the routine we will work on
float genHistogram(const int V[], int size, int H[11], int *lowCount, int *highCount) {
    float vSum = 0 ;
    for(int i=0; i<=11; ++i) {
        H[0] = 0 ;
    *lowCount = 0 ;
    *highCount = 0 ;
    for(int i=0; i<size; ++i) {
        int x = V[i] ;
        if (x < 0 ) {
            ++*lowCount ;
        } else if (x > 10) {
            ++*highCount ;
        } else {
            vSum += x;
            ++H[x] ;
    return vSum/size ;

You won’t be able to compile the code on the first attempt. You need to add the -std=c99 as an option for the GNU C compiler. (Start with FileProject Properties and add -std=c99 as an additional option for the C Compiler.)

Taking a look at the program

The program generates a randomly-sized array of random integers in main. These are passed to genHistogram which fills in a histogram giving the number of times each integer from 0 to 10 occurs in the array.

Here is a short description of the arguments to genHistogram.

The transformations

Remember: All your changes are inside the genHistogram routine.

Arrays and pointer arithmetic

In the Using pointers in C notes, you read that “P[i] is *(P+i)”. Go to the second for loop of genHistogram and modify its two array references to use the *(P+i) style. (You should never do this unless specifically ordered by your instructor or boss.)

Compile and run your program and make sure it still does its job.

You know that was really a rather silly change.

No while

Following the rules of translating C control structures, eliminate the two for loops from genHistogram.

First, transform the for loops into while loops. Do this one for at a time and run your program after each for to while transformation.

Second, transform each while loop into an if with a couple of labels. You can use almost any name you wish for a label, but I suggest something like loopstartα and loopendα. Again, run your program after each while has been eliminated.

Third, there is still and ifelseelse in your program. Get rid of the two else clauses and replace them using an extra if and some labels. You must remove every curly brace inside the routine.