You are on page 1of 4

ICS 103 (071)

HW4

Due Date: Tuesday January 8, 2008


Question 1: (60 points)
A popular method of displaying data is a Histogram. A histogram counts how many items
of data fall in each of n equally sized intervals and displays the results as a bar chart in
which each bar is proportional in length to the number of data items falling in that
interval.
Write a program that generates n random integers in the range 0-99 (use the remainder to
fix the interval from 0 to 99) and produces a Histogram from the data. Assume that we
wish to count the number of numbers that lie in each of the intervals 0-9, 10-19, 2029, ........., 90-99. This requires that we hold 10 counts, use an array to hold the 10 counts.
Having calculated the interval counts draw the Histogram by printing each bar of the
Histogram as an appropriately sized line of X's across the screen as below
0 - 9 16
10 - 19 13
20 - 29 17
etc.

XXXXXXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX

#include <stdio.h>
#include <stdlib.h>
#define SIZE 200
int main(void) {
int count[SIZE]={0},value,n,index,left,right,i,j;
printf("Enter how many values you want to generate>");
scanf("%d",&n);
for(i=0;i<n;i++) {
value=rand()%100;
index=value/10; // here students may use if-else-if that's fine
count[index]++;
}
printf("range # histogram\n");
for(i=0;i<10;i++) {
left=i*10;
right=left+9;
printf("%2d-%2d%4d ",left,right,count[i]);
for(j=0;j< count[i];j++)
printf("X");
printf("\n");
}

return 0;
}
=======================================================
// other version using if-else-if
#include <stdio.h>
#include <stdlib.h>
#define SIZE 200
int main(void) {
int count[SIZE]={0},value,n,index,left,right,i,j;
printf("Enter how many values you want to generate>");
scanf("%d",&n);
for(i=0;i<n;i++) {
value=rand()%100;
if(value>=0 && value <9)
count[0]++;
else if( value >=10 && value <19)
count[1]++;
else if( value >=20 && value <29)
count[2]++;
else if( value >=30 && value <39)
count[3]++;
else if( value >=40 && value <49)
count[4]++;
else if( value >=50 && value <59)
count[5]++;
else if( value >=60 && value <69)
count[6]++;
else if( value >=70 && value <79)
count[7]++;
else if( value >=80 && value <89)
count[8]++;
else if( value >=90 && value <99)
count[9]++;
}
printf("range # histogram\n");
for(i=0;i<10;i++) {
left=i*10;
right=left+9;
printf("%2d-%2d%4d ",left,right,count[i]);
for(j=0;j< count[i];j++)
printf("X");
printf("\n");
}
return 0;

}
Question 2: (40 points)
Write a program that reads integer numbers from a file input.txt and insert them in a 1D array. Your program will write the number, it reads, into the array only if it is not
already there. i.e. the array will not contain duplicate numbers; check the presence of the
value in the array by calling the logical function isPresent.
Write a logical function isPresent to receive the array and a value. It returns 1 if the
value is present in the array, 0 otherwise.
#include <stdio.h>
#define SIZE 100
int present (int x[], int n, int value) {
int i;
for (i=0;i<n;i++) {
if( value==x[i])
return 1;
}
return 0;
}
int main(void) {
int x[SIZE], value,status,count=0,i;
FILE *infile;
infile = fopen("input.txt", "r");
status=fscanf(infile,"%d",&value);
while(status!=EOF) {
if(!present(x,count,value)) {
x[count]=value;
count++;
}
status=fscanf(infile,"%d",&value);
}
printf("Values found\n");
for(i=0;i<count;i++)
printf("%d\t",x[i]);
return 0;
}

Instructions:
- Change your WebCT password if you have not done so. This is your responsibility.
Some cheating cases involved students getting to accounts of other students without their
knowledge.
-You need to write 1 file hw3.cpp containing all functions including the main function
-Upload the file using upload button of WebCT
- Then click on submit button of WebCT
- Submission should be no later than January 8, 2008 at midnight.
- No group work is allowed. The homework solution has to be your own work. any
cheating will lead to severe consequences.

You might also like