Question:
C++ one dimensional array question?
2007-08-01 20:40:42 UTC
I need a program that would read in array integer values (lets say 10 values) and the output would arrange them from highest to lowest value with the count of the number of occurences for each value. (Again only one dimensional array)
Three answers:
I R Sonajiso
2007-08-01 21:08:31 UTC
Ah yes, sorry. For some reason I believed the program needed to produce unique numbers. Well, this si what happens if you just glance over the specifications.



Now before providing another solution using arrays, I just have one more question. Can you use preimplemented algorithms or do you need to write a sorting function as well? If so, are there any runtime bounds?



-----------------------------------------------------------



Does it need to be an array? If not, here is a very nice and very fast way of doing just what you need.



Insertion O(n lg n); ( for n {= 10} elements )

Occurence checking: O (m) where m = # different items

Output (no repeating elements) O(m);

Output ( with repeating elements) O (n)



So here we go:



#include

#include



#define ITEMS 10



using namespace std;





int

main( int argc, char * args[] )

{

//Storage for numbers

multiset < int > values;



//Get input

for ( int i =0; i< ITEMS; i++ )

{

int number;

cout << "Well? : "<< flush;

cin >> number;

values.insert( number );

}



//Generate sorted, ascending, unique output

//For descending you would use rbegin();

multiset< int > :: iterator I_value;

I_value = values.begin();



while ( I_value != values.end() )

{

cout <<"Unique Sorted: "<<(*I_value) << "\n" << flush;

I_value = values.upper_bound( *I_value );

}



//Output all numbers in ascending order

for (I_value = values.begin(); I_value != values.end(); I_value ++)

{

cout <<"Just Sorted: "<<(*I_value) << "\n" << flush;

}



return 0;

}
?
2016-12-11 12:47:11 UTC
C++ handles an array by using in basic terms laying the products out sequentially in memory. C++ handles a multi-dimensional array as an array of arrays. An array and its first factor constantly start up interior the comparable place. An array evaluates to the handle of its first factor, it is likewise the handle of the array. to respond to your question, you had: int x[][5] = {{a million,2... this means x is an array of arrays. each and each array incorporates 5 integers. in view that x is an array of arrays, x[0] is an array. the cost of an array is the handle of its first factor. the 1st portion of x is x[0] whose first factor is x[0][0]. So the handle of x[0][0] is the comparable because of the fact the cost of x[0]. the cost of x[0][0] is a million (because of the fact it replaced into initialized to that). in view that x is an array x's value is handle of x's first factor, and because &x[0] is likewise the handle of x's first factor, x and &x[0] are the comparable. &x[0] skill the handle of x[0]. x[0] is the 1st factor interior the array, and an array's value *is* the handle of its first factor. x[0][0] is the 1st portion of the array x[0], so &x[0][0] is the handle of the 1st factor interior the array x[0], it is the handle of the 1st portion of x besides, so: x == x[0] == &x[0] == &x[0][0]. extraordinary, yet actual.
i_am_the_next_best_one
2007-08-01 22:17:27 UTC
Implement above program in array its, easy.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...