-
C (
http://www.daniweb.com/forums/forum118.html)
| Katya | Dec 10th, 2006 1:45 pm | |
| number of times each number in array occurs? 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); |
| ~s.o.s~ | Dec 10th, 2006 2:17 pm | |
| 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? |
| Katya | Dec 10th, 2006 2:34 pm | |
| 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. |
| ~s.o.s~ | Dec 10th, 2006 2:47 pm | |
| 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. |
| Katya | Dec 10th, 2006 3:09 pm | |
| 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"? |
| iamthwee | Dec 10th, 2006 3:13 pm | |
| Re: number of times each number in array occurs? Quote: Originally Posted by Katya (Post 287803) thanks a lot!
Just one more question. What do you mean by "and if it does, increment the counter by one"? | counter = counter + 1 |
| ~s.o.s~ | Dec 10th, 2006 3:16 pm | |
| Re: number of times each number in array occurs? 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. |
| shuncyk | Oct 19th, 2007 6:56 pm | |
| Storing numbers in an array, please help! #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;
} |
| dwks | Oct 20th, 2007 6:34 pm | |
| 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. |
| All times are GMT -4. The time now is 1:23 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC