Trying to write program that counts the number of times a number occurs in an array. The following is the code I have written so far. It doesn't give the correct output though. It seems to be counting the output incorrectly. Any pointers where I could be going wrong?

# include <stdio.h>
# define N 1000

int main (void)
{
	int a[N], i=0, num_int, count=0, counters[11] = {0};

	printf("Enter no. of integers to be inputted : ");
	scanf("%d",&num_int);

	for (i=0; i<num_int; i++){
		a[i] = rand() % 10 + 1; 
		}
	
	for( i = 1; i <num_int; i++ )
		++counters[a[i]];
		printf("\n");
		for (i=0; i<num_int; i++){
		
			printf("%d occurs %d times\n", a[i], counters[i]);
		}

	return 0;
}

SAMPLE OUTPUT
-------------------------------------
Enter no. of integers to be inputted : 10

2 occurs 0 times
8 occurs 1 times
5 occurs 0 times
1 occurs 1 times
10 occurs 0 times
5 occurs 3 times
9 occurs 0 times
9 occurs 0 times
3 occurs 1 times
5 occurs 2 times
Press any key to continue . . .

In your 2nd for loop i starts at 1. In the first i starts at 0. You're incrementing counters[a]
but you're displaying counters.

Apart from initializing "i" to zero in all your for loops, your final display statement should be:

printf("%d occurs %d times\n", i, counters[i]);

Hi, I have a file, and I have several lines in that file.
I want to output what the file contains.
But when i try to print it, only the first line prints, but not the rest of the lines.
Can anyone help me please.
Thank You

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int frequency[26]={0};
char common_frequency[27]={'e','t','a','o','i','n','s','h','r','d','l','c','u','m','w','f','g','y','p','b','v','k','j','x','q','z','\0'};
char cipher[27]={0};
char ciphertext[1000]={0};


int main(void)
{
char filename[80];
FILE *fp;
int n;
printf("Thank You for playing the Cryptographer Game\n");
printf("Please enter the name of the file containing the cyphertext: ");
gets(filename);
printf("The file name you entered is:\n %s\n", filename);
fp=fopen(filename, "r");
if (fp==NULL)
{
printf("We appologize. A problem occued trying to open the file.\nThe program will terminate. Press any key to continue.\n");
getchar();
exit(EXIT_FAILURE); /*If file not found, exit*/
}


fgets(ciphertext, sizeof(ciphertext), fp);
printf("this is the ciphertext, the %s contatins:\n\n%s",filename, ciphertext);
getchar();
return 0;


}

Edited 3 Years Ago by happygeek: fixed formatting

@adrianapatty_28
Start a new thread for your problem.

Do this:

printf("%d occurs %d times\n", i, counters[i]);

and change your last for loop from for(i=0;i<num_int;i++) to for(i=0;i<11;i++) .

Trying to write program that counts the number of times a number occurs in an array. The following is the code I have written so far. It doesn't give the correct output though. It seems to be counting the output incorrectly. Any pointers where I could be going wrong?

# include <stdio.h>
# define N 1000

int main (void)
{
	int a[N], i=0, num_int, count=0, counters[11] = {0};

	printf("Enter no. of integers to be inputted : ");
	scanf("%d",&num_int);

	for (i=0; i<num_int; i++){
		a[i] = rand() % 10 + 1; 
		}
	
	for( i = 1; i <num_int; i++ )
		++counters[a[i]];
		printf("\n");
		for (i=0; i<num_int; i++){
		
			printf("%d occurs %d times\n", a[i], counters[i]);
		}

	return 0;
}

SAMPLE OUTPUT
-------------------------------------
Enter no. of integers to be inputted : 10

2 occurs 0 times
8 occurs 1 times
5 occurs 0 times
1 occurs 1 times
10 occurs 0 times
5 occurs 3 times
9 occurs 0 times
9 occurs 0 times
3 occurs 1 times
5 occurs 2 times
Press any key to continue . . .

i read your logic,but it wasn't solving your problem.
In your problem you have to take a single element of array and then compare with rest of array to check if it matches so we increment the counter.I have used two loops (nested) as to compare every element with single one.

int main (void)
{
int a[N], i=0, j=0,num_int, count=0,temp=0;

printf("Enter no. of integers to be inputted : ");
scanf("%d",&num_int);

for (i=0; i<num_int; i++){
a = rand() % 10 + 1;
printf("array elelments are%d\n",&a);
}

for( i = o; i <num_int; i++ )
{
temp=a;
for( j = o; i <num_int; j++ )

{
if(temp==a)
count++;
}
printf("%d occurs %d times\n", a, count);
count=0;
}


return 0;
}

i read your logic,but it wasn't solving your problem.
In your problem you have to take a single element of array and then compare with rest of array to check if it matches so we increment the counter.I have used two loops (nested) as to compare every element with single one.

int main (void)
{
int a[N], i=0, j=0,num_int, count=0,temp=0;

printf("Enter no. of integers to be inputted : ");
scanf("%d",&num_int);

for (i=0; i<num_int; i++){
a = rand() % 10 + 1;
printf("array elelments are%d\n",&a);
}

for( i = o; i <num_int; i++ )
{
temp=a;
for( j = o; i <num_int; j++ )

{
if(temp==a)
count++;
}
printf("%d occurs %d times\n", a, count);
count=0;
}


return 0;
}

You have used fgets() function which is only read data from the file until new don't occur,when it find a new line it stops reading .so to read the entire file add following code in your code:-

char s;
while(s!=EOF)
{
s=fgetc(fp); //your file pointer
printf("%c",s);
}

EOF is a special character which occurs in the end of file,it is an indication of end of file.

Some notes about previous post.
1. The fgets function was supposed but an example used another function fgetc. It defines while loop incorrectly because s variable is uninitialized at the start loop moment. This loop icorrectly prints possible on eof returned value (see #2).
2. EOF is not "a special character which occurs in the end of file". It's a macros defined int value (as usuallty negative) which differs from any char values might be occured in a file. That's why fgetc returns int but not char value. The previous post code snippet, strictly speacking, defines loop forever in C implementations where char type is unsigned by default.

What's a dubious help ;)...

I would advice threadstarter to read post #2, #3 and #5. The rest are either incorrect or referred to another question.

To adrianapatty_28, please start another thread if you have a totally different question. Its making this thread messy.

The simple and effective the original problem solution: input data, sort them - now pass over sorted array, count and print all different values ;)...

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