User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,655 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,790 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2310 | Replies: 8
Reply
Join Date: Dec 2006
Posts: 3
Reputation: Katya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Katya Katya is offline Offline
Newbie Poster

number of times each number in array occurs?

  #1  
Dec 10th, 2006
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!!!!
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. main()
  5. {
  6. float avg,sum=0;
  7. int i,j;
  8. int numbers[20]; /*array declaration*/
  9. for(i=0;i<20;i++)
  10. {
  11. printf("\nEnter number: ");
  12. scanf("%d",&numbers[i]); /*store data in array*/
  13. }
  14. for(i=0;i<20;i++)
  15. sum=sum+numbers[i]; /*read data from array*/
  16. avg = sum/20;
  17. printf("\n\nAverage number of this 20 numbers is %5.2f\n\n\n",avg);
Last edited by ~s.o.s~ : Dec 10th, 2006 at 1:12 pm. Reason: Added code tags learn to use them yourself.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,774
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: number of times each number in array occurs?

  #2  
Dec 10th, 2006
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?
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2006
Posts: 3
Reputation: Katya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Katya Katya is offline Offline
Newbie Poster

Re: number of times each number in array occurs?

  #3  
Dec 10th, 2006
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.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,774
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: number of times each number in array occurs?

  #4  
Dec 10th, 2006
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 [i]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 [i]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.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2006
Posts: 3
Reputation: Katya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Katya Katya is offline Offline
Newbie Poster

Re: number of times each number in array occurs?

  #5  
Dec 10th, 2006
thanks a lot!

Just one more question. What do you mean by "and if it does, increment the counter by one"?
Reply With Quote  
Join Date: Aug 2005
Posts: 4,663
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: number of times each number in array occurs?

  #6  
Dec 10th, 2006
Originally Posted by Katya View Post
thanks a lot!

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


counter = counter + 1
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,774
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: number of times each number in array occurs?

  #7  
Dec 10th, 2006
What I mean is :
  1. // if the element of array matches with another element in the same
  2. // array, then increment the counter.
  3.  
  4. if( my_array[i] == my_array[j] )
  5. {
  6. ++counter ; // counter = counter + 1
  7. }
  8.  
  9. // my_array = {1, 2, 1, 3, 4, 5 }
  10. // my_array[i] = my_array[0] = 1
  11. // counter = 2 ( since 1 has a match at 0th and 2nd position )

Hope it helped, bye.
Last edited by ~s.o.s~ : Dec 10th, 2006 at 2:17 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Oct 2007
Posts: 3
Reputation: shuncyk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
shuncyk shuncyk is offline Offline
Newbie Poster

Storing numbers in an array, please help!

  #8  
Oct 19th, 2007
#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;

  }
Last edited by ~s.o.s~ : Oct 20th, 2007 at 11:22 pm. Reason: Added code tags
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 234
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: number of times each number in array occurs?

  #9  
Oct 20th, 2007
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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 1:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC