•
•
•
•
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
![]() |
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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!!!!
I have sorted average number but straggle with "number of occurrences". Please help!!!!
c Syntax (Toggle Plain Text)
#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);
Last edited by ~s.o.s~ : Dec 10th, 2006 at 1:12 pm. Reason: Added code tags learn to use them yourself.
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?
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."
"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."
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.
"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."
"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."
What I mean is :
Hope it helped, bye.
c Syntax (Toggle Plain Text)
// 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.
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."
"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."
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
#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
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.
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
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
- Trying to creating an array from a text file (C++)
- Number of times a number appears in an array (C++)
- Wierd error messages with calculator program (C++)
- SEO and AdWords API (Pay-Per-Click Advertising)
Other Threads in the C Forum
- Previous Thread: reading in from vector to a two dimensional array
- Next Thread: Question about my project



Linear Mode