Question

In: Computer Science

Write a program IN C that reads all integers that are in the range of 0...

Write a program IN C that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file.

Note: The program ignores any number less than 0 or greater than 100.

Note: Do not display zero if a number is not in the file.

Hints: An array of size 101 is good enough. A number in the file plays the role of an index.


For example: Suppose the content of the file: a.txt is as follows:
99 2 99

3

-12 80 12 33

3 99 100 1234 84


The display output is:
2 has occurred: 1 times

3 has occurred: 2 times

12 has occurred: 1 times

33 has occurred: 1 times

80 has occurred: 1 times

84 has occurred: 1 times


99 has occurred: 3 times

100 has occurred: 1 times


Note that this is just a sample dialog. Your program should work for any number of data values and you MUST match the sample dialog as well.

Solutions

Expert Solution


/*
 *  C Program to count number of occurence of 0-100 numbers in text file
 */


#include <stdio.h>
#define MAX 101

int main()
{
  int i, num;
  int numbers[MAX] = {0};

  FILE *fRead;
  
  fRead = fopen("a.txt", "r");
  if (fRead == NULL)
  {
    printf("File not found\n");
  }
  else
  {
    while (!feof(fRead))
    {
      fscanf(fRead, "%d", &num);
      if (num >= 0 && num <= 100)
        numbers[num] += 1;
    }

    for (i = 0; i < MAX; i++)
    {
      if (numbers[i] != 0)
      {
        printf("%d has occured: %d times\n", i, numbers[i]);
      }
    }
  }
  
  fclose(fRead);

  return 0;
}


Related Solutions

write a c++ program in which for loop iterates over all integers in the range 0...
write a c++ program in which for loop iterates over all integers in the range 0 to 99 inclusive and prints out all of those numbers dividible by both 2 and 5.
1. Write a C++ program, for.cpp, that reads three integers. The first two specify a range,...
1. Write a C++ program, for.cpp, that reads three integers. The first two specify a range, call them bottom and top, and the other gives a step value. The program should print four sequences using for loops: Each value in the range from bottom to top Each value in the range from top to bottom, reverse counting Every second value in the range from bottom to top Every value from bottom to top increasing by step a: If bottom is...
a)     Write a program that reads a list of integers, and outputs all even integers in the...
a)     Write a program that reads a list of integers, and outputs all even integers in the list. For example, if the input list is [1, 2, 13, 14, 25], the output list should be [2, 14]. b)    Based on your code for part (a), write a program that reads a list of integers, and reports True if all numbers in the list are even, and False otherwise. Example: For input [1, 2, 13, 14, 25], the output should be False. Your...
Plase, write program in c++. IP address consists of four integers of range [0, 255] separated...
Plase, write program in c++. IP address consists of four integers of range [0, 255] separated by dots. The next three rows show three correct IP-address: 130.0.0.0 193.197.0.01 255.00.255.255 Write a program that determines whether a given string is a valid IP-address. outpot should be 1 if given IP address is valid, or 0 otherwise.
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
Write a C program that reads three integers and then prints them in the order read...
Write a C program that reads three integers and then prints them in the order read and reversed. Use four functions: main, one to read the data, one to print them in the order read, and one to print them reversed.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT