I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index » Programming » C/C++ »

lucian\′s Photo
27 Oct 06, 6:43AM
Hi again and sorry for not being too consistent with the code. I have mixed C with C++ and you have saved the file using a .C extension which enabled the compiler to compile the code in plain C mode. In this mode the new and delete operators are not recognized, therefore you have received those errors. There are two solutions to this:

1. Rename the file into one with a .cpp extension and then compile again, it should work well 2. Use the following version which is now written entirely in plain C mode, and save it using a .c extension.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
  // input no. of students
  int n, i;
  printf("no. of students = "); scanf("%d", &n);
 
  // allocate an array for exactly "n" marks
  int *marks = (int*) malloc(n * sizeof(int));
 
  // input marks
  printf("marks: ");
  for (i = 0; i < n; i++)
    scanf("%d", &marks[i]);
 
  // compute frequencies
  int freq[101] = {0};
  for (i = 0; i < n; i++)
    freq[marks[i]]++;
 
  // display non-zero frequencies
  printf("\nMark Frequency\n\n");
  for (i = 0; i < 101; i++)
    if (freq[i])
      printf("%d %d\n", i, freq[i]);
 
  // display all intervals between identical marks
  printf("\nIntervals\n\n");
  int pos[101] = {0};
  for (i = 0; i < n; i++)
  {
    if (pos[marks[i]])
      printf("%d %d\n", marks[i], i - pos[marks[i]]);
    pos[marks[i]] = i + 1;
  }
  printf("\n");
 
  // free memory used by the marks[] array
  free(marks);
 
  return 0;
}

Currently you need to be logged in to leave a message.