Hi everyone! I need to write program that will find the average of 20 element array and also display the number of each number in the array occurs.
I have sorted average number but straggle with "number of occurrences". Please help!!!!

#include<stdio.h>
#include<math.h>
 
main()
{
float avg,sum=0;
int i,j;
int numbers[20]; /*array declaration*/
for(i=0;i<20;i++)
{
printf("\nEnter number: ");
scanf("%d",&numbers[i]); /*store data in array*/
}
for(i=0;i<20;i++)
sum=sum+numbers[i]; /*read data from array*/
avg = sum/20;
printf("\n\nAverage number of this 20 numbers is %5.2f\n\n\n",avg);

Recommended Answers

All 9 Replies

First of all use int main( void ) and not just main( ) since its not standard.

Don't hard code values in your program, if you want to process 20 values make a preprocessor defination at the start of your program which can easily be modified if you are asked to change the requirements. Using magic numbers as such causes a lot of confusion.

Can you give us a sample run or a dummy example of what kind of output you are expecting ? Are you required to store the occurances or just display them?

Thank you for your reply!

This program should be displayed on the screen in two columns, i.e col.1: the number and col.2: the number of occurences.

Hmm..if thats the case then all you are required to do is to use nested loops. Here is a short algorithm:

  • Create a variable count which will keep track of the occurances of the numbers.
  • Start an outer loop with i as the counter or index, initialize it with 0 and continue looping till i is less than the number of elements or till all the elements have been visited.
  • Create an inner loop with j as the counter, initialize it in the same manner as the previous loop.
  • Inside the inner loop check if my_array equals my_array[j] and if it does, increment the counter by one.
  • Close the inner for loop and after the loop completion, print out the value of counter which should be the number of times the element my_arrayhas occured along with the element under consideration.
  • Reset the counter value back to zero for the remaining elements.
  • Keep looping the outer loop till all the elements have been visited.
  • End.

Hope it helped, bye.

thanks a lot!

Just one more question. What do you mean by "and if it does, increment the counter by one"?

Member Avatar for iamthwee

thanks a lot!

Just one more question. What do you mean by "and if it does, increment the counter by one"?

counter = counter + 1

What I mean is :

// if the element of array matches with another element in the same 
// array, then increment the counter.

if( my_array[i] == my_array[j] ) 
{
     ++counter ; // counter = counter + 1
}

// my_array = {1, 2, 1, 3, 4, 5 }
// my_array[i] = my_array[0] = 1 
// counter = 2 ( since 1 has a match at 0th and 2nd position )

Hope it helped, bye.

#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>
  
ofstream fout;                                    // output textfile declaration
using namespace std;

// =============================================================================

int main ()

  {
    int i, count;
    char reply;
    double x[100];

    fout.open ("array.txt");                    // prepares output textfile

    // introduction

    for (i = 1; i <= 9; i++)
      {
        cout << endl;
      }
    cout << setw (52) << "STORING DATA IN AN ARRAY" << endl;
    cout << endl << endl;
    cout << setw (66) << "This program will prompt the user for a set of numbe";
    cout << "rs:" << endl;
    cout << endl << endl;
    cout << setw (56) << " Press ENTER to continue ... ";
    cin.get ();
    
    // prompting the user for a set of numbers
    
    count = 0;

    fout << setw (52) << "Data stored in array";
    fout << endl << endl;

    do // while there is more input data
       {
         count++;
     
    do // while data is incorrect
       {
         clrscr ();
          
         for (i = 1; i <= 11; i++)
              {
                cout << endl;
              }

         cout << setw (56) << "Enter data " << count << "or enter -1 to end";
         cout << " data entry:";
         cin >> x [count];

    if (count % 25 == 0)

        {
         
         fout << "\f";
         fout << setw (45) << "Values Stored in an Array";
         fout << endl << endl;
         
        } 
            do // while response is invalic
              {
                clrscr ();

                for (i = 1; i <= 11; i++)
                  {
                    cout << endl;
                  }

            while (y != -1);
              }
   do // record verified data in output textfile
      {
        fout << setw (32) << "#" << count << ")" << setw (5) << count;
        fout << endl << endl;

      do // while response is invalid
          {
            clrscr ();

            for (i = 1; i <= 9; i++)
              {
                cout << endl;
              }
          }
   while (count != -1)
       }

    cin.get ();
    fout.close ();                                     // closes output textfile
    return 0;

  }

Use code tags when you post code -- otherwise it will be an unreadable mess.

This is also the C programming forum, not the C++ one, so C++ code is out of place.

It's also old-style C++ code. You're using <iostream.h> and <fstream.h> etc, which are pre-standard. Use <iostream> and <fstream> instead. Also, you're declaring an ofstream before the using namespace std, which shouldn't work if you use the right header files. Declare it afterwards.

Hi friends i want the perfect code to find the number of occurance of each number in an array?(in c/c++/java)plz provide comments to understand
Ex:output should be as follows
Enter values
1,1,2,3,4,4

1->2
2->1
3->1
4->2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.