#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main() {
  int tempnum = 0;
  int i;
  int mainnum  ;
  int maincount = 0;
  int tempcount ;
  int *a;
  int n;
  
  FILE * file;
  file = fopen ("input.txt", "r");

  if(file==NULL) {
    printf("Error: can't open file.\n");
    return 1;
  }
  else {
    printf("File opened successfully. Contents:\n\n");
    
    while(feof(file)== 0) { 
    for ( i=0; i<n ; i++)
    fscanf(file, "%s", &a[i]);
    }
    fclose(file);
    
                    for ( i=0; i<n-1 ; i++) {
                            
                            if ( a[i] == tempnum){
                                 tempcount = tempcount + 1;}
                                 else{
                                 tempnum = a[i];
                                 tempcount = 1;
                                 }
                             
                            if (tempcount > maincount) {
                                 maincount = tempcount;
                                 mainnum = tempnum;
                                 }}
                         printf(" %s" ,mainnum );
                         printf(" %s ", maincount);  
    getch ();

    return 0;
    
}}

example
input.txt 5 3 7 7 7 7 4 3 3 9 9 9 9 9 9 9 8 9 0

0 indicates the end of data, and number range from 1 to 9.program should read numbers from input.txt and output should be number appear the most one after the other and number of times eg for the above 9 7.

Edited 6 Years Ago by cyberguy007: n/a

There's so much wrong
1) you input the line into a pointer with no space, overwriting who-knows-what
2) you use feof() to control the loop -- see this
3) formatting, though not disgusting, isn't great.
4) you never count the occurrence of any values, you just add 1 every time you happen to see a number two or more time in succession.

Set up an array to keep track of how many times a single digit is seen.
When you see a digit, increment the corresponding array position.
You can read a character at a time rather than a full line.

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