im writing a program that creates a dynamic array then input numbers(not exceeding 100).

i used new int to create the array. im running into the problem that it only allows 21 numbers to be entered, then does sort,mean. could anyone explain where im going wrong setting up the array or in it usage?

on my specifications(its homework) it shows what functions we need to have in the finished product,
float mean(const int array, size_t size)*. could anyone explain what size_t is in realtion to this function??

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

void sort(int a[], int n);
float mean(int a[], int n);

int main(int argc, char *argv[])
{
    int n,g,h;
    int size;

    int r;
    int x;
    int i=0,count=0,v=0;


    int* array;
    array = new int [size];

   for(g=0; g<size ;g++) // sets array to 0
    {
         array[g]=0;      
    }

    cout << "enter intger values" << endl;
 do{

         for(i= 0; i < size ; i++) // number input
         {          
              cin>>array[i]; 
         }

   }while(x >'/' && x<':'); 


    sort( array,size); // sorts numbers

    cout << "\n\n\n\n";

    for(int i=0;i < size; i++) //shows sorted array
    {
        cout <<array[i]<<endl; 
    }
    mean(array,size);    

    system("PAUSE");
    return EXIT_SUCCESS;
}
void sort(int a[], int n) 
{
      int i, j, index, z;    
      for (i = 0; i < n - 1; i++) 
      {
            index = i;
            for (j = i + 1; j < n; j++)
                  if (a[j] < a[index])
                        index = j;
                        if (index != i) 
                        {
                           z = a[i];
                           a[i] = a[index];
                           a[index] = z;
                        }
      }
}
float mean(int a[], int n)
{
     int i;
     double sum;
     double avg;
     for(i;i<n;i++)
      {
          sum+=a[i];        
      }                 
     avg =((sum)/(n));
     cout << "sum: " << sum<<endl;
     cout << "avg: " << avg<<setprecision(2)<<setw(2)<<endl; 
     cout << "\n\n\n";     
}

Recommended Answers

All 13 Replies

array = new int [size];
Wrong syntax. Look up the correct syntax again.

Also, what is the value of size at that point of the program? Hint: It's probably not zero.

You really really need to make sure all variables are initialised before using them:

int iGeorge ;
int iAlbert = 100 ;

// At this point in the program we have no idea what iGeorge is and you should
// not use it until you give it a value... iAlbert is fine.

iGeorge = 0 ; // NOW you can safely use iGeorge

It is a "feature" of C and C++ that variables are not initialised unless you do so explicitly, as in the iAlbert example.

int *array = new int[size]; is this correct?

i also google around and found
const size_t size=100; is this the right why to us size_t? i used 100 as the max input

First, define the value for 'size'. The const size_t size=100; statement is fine (proper even). After that, you can instantiate array as in your code int *array = new int[size];. At that point, your loop should work. As the others pointed out, in your first code example, size was not initialized, so it had whatever was in memory at that address at the time, which just happened to be 21. It could have been any other number up to INT_MAX (+2 billion or so), or down to INT_MIN (-2 billion or so)...

ok, thank you! my main problem is even though i know it will not exceed 100 numbers, how do i make the loop stop when i enter a non-digit number? right now when i input say 6 numbers, it divdes the sum of those 6 by 100. when really i just want it to divide the sum by 6.

any tips to help me?

Count them as you input them

im writing a program that creates a dynamic array then input numbers(not exceeding 100).

Why do you need an array with dynamic storage duration? You know the size of the array at compile time - 100 - and that is not a huge number.

constexpr std::size_t ARRAY_SIZE = 100 ;
int array[ ARRAY_SIZE ] ;

std::size_t count = 0 ; // holds the count of numbers entered. 
// invariant: count <= ARRAY_SIZE

This loop is completely wrong.

    cout << "enter intger values" << endl;
 do{

         for(i= 0; i < size ; i++) // number input
         {          
              cin>>array[i]; 
         }

   }while(x >'/' && x<':');

What will happen if the user enters a number (say 1) which compares less than '/' or 1234567 which compares greater than ':' ?

Instead, something like this:

cout << "enter intger values (enter any non-integer to stop):\n" ;
while( ( count < ARRAY_SIZE ) && ( std::cin >> array[count] ) ) ++count ;

i know it could do that, but my prof. said we had to use a dynamic arry, in reality i would use a vector.

i ended up rewriting the program to do a sentinel loop, and count as i go like
WaltP said.

it runs, but my swap is not working :/

the prof. gave us this void swap(int& x, int& y)

which means i have to break up my seletion sort now, i thought that was going to be easy, but its not :(

void sort(int* array,int size)
{
      int a,x, j, y, z;    
      for (x = 0; x < size - 1; x++) 
      {
            y = x;
            for (j = x + 1; j < size; j++)
                  if (array[j] < array[y])
                        y = j;
                        if (y != x) 
                        {
                           z = array[x];
                           array[x] = array[y];
                           array[y] = z;
                        }
      }
}
// heres what i have now with the swap()
void sort(int* array,int size)
{
      int a,x, j, y, z;    
      for (x = 0; x < size - 1; x++) 
      {
            y = x;
            for (j = x + 1; j < size; j++)
                  if (array[j] < array[y])
                        y = j;
                        if (y != x) 
                        {
                           swap(x,y);
                        }
      }
}
void swap(int& x, int& y)
{    int z;
     z = x;
     x = y;
     y = z;
}
// any sugestions why its not sorting at all now? 

That is a bubble sort, not a selection sort.

Get rid of the variable y, it's getting in your way.

When you call swap() what values are you supposed to pass to it? The index of the values, or the values themselves?

im passing int &x, int &y by reference. get rid of the y in the 'swap()'?

And what is in x and y? Look carefully at your code. It is NOT what you think. And even if it was, how would that affect the array itself?

at this point x and y are anything, before it checked the elements of the array.

edit: would swap the address of the value?

I claim your swap is not doing what you want it to do. So, here's your first lesson in debugging.

I am assuming you want to swap two elements of the array. (If this is not true, ignore the rest of this post.)
I claim it is not performing the function you want.

Before the call to `swap()`, add a `for()` loop to output the entire array, followed by a newline.
After the call to `swap()`, add a `for()` loop to output the entire array, followed by `getchar()` to stop the program.
Compare the two outputs.
Did it swap?
If so, fine. If not, why?

Outputting values like this is the easiest way to debug a program rather than waiting hours for one of us to fix it for you. It doesn't always work, but it's a lot better than just changing code at random without thinking and hoping for the best.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.