Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

pointers and array structures

$
0
0
Using only pointers, I need to read numbers in from a file, which I do using emacs. That part is fine and works great. But I now need to make an array of structs, and again using pointers, pass the struct to calculateHistogram and fill it in so that it produces an output like this when I display it :

value 10: freq 3
value 8: freq 2
value 7: freq 3
value 5: freq 1
value 4: freq 1
value 2: freq 2

I need to fill each struct with the number and how many times it occurs in the tiny file I'm reading in. Any assistance with this would be great, I'm pretty well stuck at the moment. By the way, there are only 12 numbers in the file, but please don't design anything to work with just 12 numbers, rather any set of numbers thrown at it

#include <stdio.h>

struct freq {
	int number;
	int frequency;
};

//function prototypes
void readScores();
void displayScores();
void calculateHistogram();

int main() {
	int ar[100];
	int count = 0;
	
	readScores(ar, &count);
	displayScores(ar, &count);
	
	struct freq hist[count];
	printf("\n");
	calculateHistogram(ar, &count, hist);
	
}

void readScores(int* ar, int* count) {
	for (int i=0; i<12; i++) {
		(*count)++;
		scanf("%d", (ar+i));
	}
}

void displayScores(int* ar, int* count) {
	for(int i=0; i<*count; i++) {
		printf("score %d: %d\n", i, *(ar+i));
	}
}
void calculateHistogram(int* ar, int* count, struct freq* hist) {
	for(int i=0; i<*count; i++){
	if((*(ar+i)) != (*(hist+i).number){
	 ??????????????????????
		}
	}
	
}

 

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>