Hi everyone, this is my first post on this forum. I've been browsing these boards for awhile and thought it would be a good idea to register now, for i hope someone can help me out :)

I've created a program which enables a user to input numbers from 1 to 10. First it asks for the user to specify the amount of numbers that are going to be put in. Then the numbers are inserted and stored in an array.

After that a frequency array get's made from the user's input and a histogram gets printed. So far, so easy :)

Here's the code for my program:

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

int main(int argc, char *argv[]) {
  int numberRow[100];
  int frequenties[11];

  int numbers, loopNumber = 0;
  scanf("%d", &numbers);

  do {
	scanf("%d", &numberRow[loopNumber]);
	loopNumber++;
  } while (loopNumber < numbers);
  
	int x;

 for(loopNumber = 0; loopNumber<numbers; loopNumber++) {
	for(x = 1; x <= 10; x++) {
		if(numberRow[loopNumber]==x) {
			frequencies[x]++;
		}
	}
 }

int highestNumber = 0;
for(x=1; x<= 10; x++) {
	if(frequenties[x]>highestNumber) highestNumber=frequenties[x];
}

int y;
for(x=highestNumber; x>0; x--) {
	for(y=1; y<=10; y++) {
		if(frequenties[y]< highestNumber && x > frequenties[y]) {
			if(y==10)
				printf(".");
			else
				printf(". ");
		}
	  	else if(frequenties[y]<= highestNumber && x <= frequenties[y]) {
			if(y==10)
				printf("*");
			else
				printf("* ");
		}
	}
	printf("\n");
}
printf("1 2 3 4 5 6 7 8 9 10\n");

  return 0;
}

Resulting in the following output, given the input:
[IMG]http://i36.tinypic.com/2chgn8.png[/IMG]

However, now i'm asked to make the same program, but from unfinished source-code:

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

int *readNumbers() {
  int *numbers;
  /* To-Do: implement body */
  return numbers;
}
	
void getFrequency(int numbers[], int freq[11]) {
  /* To-Do: implement body */
}

int arrayMax(int length, int row[]) {
  int i;
  int max = row[0];
  for (i=1; i<length; i++) {
    if (row[i] > max) {
      max = row[i];
    }
  }
  return max;
}
	

void printHistogram(int frequenties[11]) {
  /* To-Do: implement body */

}

int main(int argc, char *argv[]) {
  int *numbers;
  int frequencies[11]

  numbers = readNumbers();
  /* To-Do: finish the rest */

  return 0;
}

I'm very confused with this 'setup', for it insists on using pointers (?) and as far as i can see the premade (but unfinished) getFrequency() and printHistogram() functions are missing an extra argument (the size of the frequency-array) in order to get a working program.

I've been looking at the code for several hours but so far i'm unable to find a way to make this same program, in that different way.

Any input will be much appreciated! :)

Cheers,
Stuart

Here you go.. I have not done that much testing.. plz go thro.. also dont forget to delete the memory allocated.. i leave it to you..

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

int *readNumbers() 
{
  int *numbers;
  numbers=(int*)malloc(sizeof(int));
  scanf("%d",numbers);
  return numbers;
}
	
void getFrequency(int numbers[], int freq[11]) 
{
	int loopnumber=freq[10];
	for (int i=0;i<loopnumber;i++)
	{
		scanf("%d",&numbers[i]);
	}


	for (i=0;i<loopnumber;i++)
		for(int x=1;x<10;x++)
		{
			if(numbers[i]==x)
			{
				freq[x]++;
			}
		}

}

int arrayMax(int length, int row[]) {
  int i;
  int max = row[0];
  for (i=1; i<length; i++) {
    if (row[i] > max) {
      max = row[i];
    }
  }
  return max;
}
	

void printHistogram(int frequenties[11]) 
{
	int highestNumber = arrayMax(10,frequenties);
	int x;


int y;
for(x=highestNumber; x>0; x--) {
	for(y=1; y<=10; y++) {
		if(frequenties[y]< highestNumber && x > frequenties[y]) {
			if(y==10)
				printf(".");
			else
				printf(". ");
		}
	  	else if(frequenties[y]<= highestNumber && x <= frequenties[y]) {
			if(y==10)
				printf("*");
			else
				printf("* ");
		}
	}
	printf("\n");
}
printf("1 2 3 4 5 6 7 8 9 10\n");


}

int main(int argc, char *argv[]) {
  int *numbers;
  int frequencies[11];

  numbers = readNumbers();
  int *array;
  array=(int*)malloc(sizeof(int)*(*numbers));
  for (int x=0;x<=10;x++)
  {
	  frequencies[x]=0;
  }
  frequencies[10]=*numbers;

  getFrequency(array,frequencies);
  arrayMax(10,frequencies);
  printHistogram(frequencies);



  return 0;
}

Thanks,
Saphar

Thank you very much saphar!

I got a few "error: \u2018for\u2019 loop initial declaration used outside C99 mode" messages when compiling but that was solved easily by initializing the variables outside loops.

Cheers! I'll have to go over the code now and see how exactly you did it :)

This article has been dead for over six months. Start a new discussion instead.