number of times each number in array occurs?

Reply

Join Date: Dec 2006
Posts: 3
Reputation: Katya is an unknown quantity at this point 
Solved Threads: 0
Katya Katya is offline Offline
Newbie Poster

number of times each number in array occurs?

 
0
  #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 2:12 pm. Reason: Added code tags learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: number of times each number in array occurs?

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: Katya is an unknown quantity at this point 
Solved Threads: 0
Katya Katya is offline Offline
Newbie Poster

Re: number of times each number in array occurs?

 
0
  #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 Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: number of times each number in array occurs?

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: Katya is an unknown quantity at this point 
Solved Threads: 0
Katya Katya is offline Offline
Newbie Poster

Re: number of times each number in array occurs?

 
0
  #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 Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: number of times each number in array occurs?

 
0
  #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
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: number of times each number in array occurs?

 
0
  #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 3:17 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: shuncyk is an unknown quantity at this point 
Solved Threads: 0
shuncyk shuncyk is offline Offline
Newbie Poster

Storing numbers in an array, please help!

 
0
  #8
Oct 19th, 2007
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: number of times each number in array occurs?

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC