944,173 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6606
  • C RSS
Dec 10th, 2006
0

number of times each number in array occurs?

Expand Post »
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 2:12 pm. Reason: Added code tags learn to use them yourself.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Katya is offline Offline
3 posts
since Dec 2006
Dec 10th, 2006
0

Re: number of times each number in array occurs?

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?
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 10th, 2006
0

Re: number of times each number in array occurs?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Katya is offline Offline
3 posts
since Dec 2006
Dec 10th, 2006
0

Re: number of times each number in array occurs?

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 10th, 2006
0

Re: number of times each number in array occurs?

thanks a lot!

Just one more question. What do you mean by "and if it does, increment the counter by one"?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Katya is offline Offline
3 posts
since Dec 2006
Dec 10th, 2006
0

Re: number of times each number in array occurs?

Click to Expand / Collapse  Quote originally posted by Katya ...
thanks a lot!

Just one more question. What do you mean by "and if it does, increment the counter by one"?
counter = counter + 1
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 10th, 2006
0

Re: number of times each number in array occurs?

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 3:17 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 19th, 2007
0

Storing numbers in an array, please help!

  1. #include <conio.h>
  2. #include <fstream.h>
  3. #include <iomanip.h>
  4. #include <iostream.h>
  5.  
  6. ofstream fout; // output textfile declaration
  7. using namespace std;
  8.  
  9. // =============================================================================
  10.  
  11. int main ()
  12.  
  13. {
  14. int i, count;
  15. char reply;
  16. double x[100];
  17.  
  18. fout.open ("array.txt"); // prepares output textfile
  19.  
  20. // introduction
  21.  
  22. for (i = 1; i <= 9; i++)
  23. {
  24. cout << endl;
  25. }
  26. cout << setw (52) << "STORING DATA IN AN ARRAY" << endl;
  27. cout << endl << endl;
  28. cout << setw (66) << "This program will prompt the user for a set of numbe";
  29. cout << "rs:" << endl;
  30. cout << endl << endl;
  31. cout << setw (56) << " Press ENTER to continue ... ";
  32. cin.get ();
  33.  
  34. // prompting the user for a set of numbers
  35.  
  36. count = 0;
  37.  
  38. fout << setw (52) << "Data stored in array";
  39. fout << endl << endl;
  40.  
  41. do // while there is more input data
  42. {
  43. count++;
  44.  
  45. do // while data is incorrect
  46. {
  47. clrscr ();
  48.  
  49. for (i = 1; i <= 11; i++)
  50. {
  51. cout << endl;
  52. }
  53.  
  54. cout << setw (56) << "Enter data " << count << "or enter -1 to end";
  55. cout << " data entry:";
  56. cin >> x [count];
  57.  
  58. if (count % 25 == 0)
  59.  
  60. {
  61.  
  62. fout << "\f";
  63. fout << setw (45) << "Values Stored in an Array";
  64. fout << endl << endl;
  65.  
  66. }
  67. do // while response is invalic
  68. {
  69. clrscr ();
  70.  
  71. for (i = 1; i <= 11; i++)
  72. {
  73. cout << endl;
  74. }
  75.  
  76. while (y != -1);
  77. }
  78. do // record verified data in output textfile
  79. {
  80. fout << setw (32) << "#" << count << ")" << setw (5) << count;
  81. fout << endl << endl;
  82.  
  83. do // while response is invalid
  84. {
  85. clrscr ();
  86.  
  87. for (i = 1; i <= 9; i++)
  88. {
  89. cout << endl;
  90. }
  91. }
  92. while (count != -1)
  93. }
  94.  
  95. cin.get ();
  96. fout.close (); // closes output textfile
  97. return 0;
  98.  
  99. }
Last edited by ~s.o.s~; Oct 21st, 2007 at 12:22 am. Reason: Added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shuncyk is offline Offline
3 posts
since Oct 2007
Oct 20th, 2007
0

Re: number of times each number in array occurs?

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.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Oct 5th, 2010
-1
Re: number of times each number in array occurs?
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alekhya.p123 is offline Offline
1 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with atoi
Next Thread in C Forum Timeline: C program for writing test cases





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC