cant pring my array in reverse order

as you can probably tell im almost lost as hell, i need to print an array in reverse order... the very last function i have is what im trying to use to do this, but its not working... help me please! thank you in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Practice with arrays for lab

#include <iostream>
#include <ctime>
#include <iomanip>
#include <string>

using namespace std;

void printArray ( const int [],  int );
void initializeArray( int  anArray [],  int size );
void randomizeArray( int  anArray [],  int size );
void printBackwards (const int  anArray [],  int size);
//printBackwards function
//targetCount function
//cubeArray function
//findHighest function

////////////////////////////////////////////////////////////////////
int main() 
{   
    const int SIZEA = 15;
    const int SIZEB = 15;      //ADDED THIS
    
    int alpha_List [SIZEA];
    int beta_List [SIZEB];  //ADDED THIS
  
     initializeArray ( alpha_List,  SIZEA );
     cout << "\nalpha_List initialized by user input\n";  
     printArray ( alpha_List, SIZEA);
     
     randomizeArray ( alpha_List,  SIZEA ); 
     cout << "\nalpha_List randomized!\n";   
     printArray ( alpha_List, SIZEA );
     
     initializeArray ( beta_List,  SIZEB );  //added this
     cout << "\nbeta_List initialized by user input\n";  
     printArray ( beta_List, SIZEB);
     
     randomizeArray ( beta_List,  SIZEB );    //added this
     cout << "\nbeta_List randomized!\n";   
     printArray ( beta_List, SIZEB );
     

     
     
   cout << endl<< endl;      
   system("pause");
   return 0;
}
////////////////////////////////////////////////////////////////////
// prints out the entries in an array
void printArray ( const int  anArray [],  int size )
{    
     static int count = 0;
     int looper ;
     cout<<"\n---------------\n";
     
     for (looper =0;looper <size;looper++)
            cout   <<  setw(5) <<anArray [looper] <<endl;
     count++;
     
      cout <<"This print function has been called "<<count<<" times.\n";
      cout<<"\n=============================\n";
           
}  
////////////////////////////////////////////////////////////////////
// fills an array with random numbers between 0 and 50, inclusive 
void randomizeArray( int  anArray [],  int size )
{
    int num ;
    int looper;
    for ( looper = 0; looper < size ; looper++)
    {
        num = (rand () + time (0) ) % 51;
        anArray[looper] = num;
    }       
}     
////////////////////////////////////////////////////////////////////
void initializeArray( int  myArray [],  int size )
{
    int value;        // number used for initializing each array location
    cout <<"\n\nEnter an integer to initialize the array components:  ";
    cin >> value; 
    int looper;
    for ( looper = 0; looper < size ; looper++)
    {
        myArray[looper] = value;
    }       
    

void printBackwards ( int alpha_List,  int beta_List )
{    
     static int count = 15;
     int looper ;
     cout<<"\n---------------\n";
     
     for (looper =0;looper <size;looper--)
            cout   <<  setw(5) <<anArray [looper] <<endl;
     count--;
    
}     

////////////////////////////////////////////////////////////////////

Well, look at that for-loop:
1
2
 for (looper =0;looper <size;looper--)
            cout   <<  setw(5) <<anArray [looper] <<endl;

The first time around, that will simplify to:
cout << anArray[0] << endl;
Then, the next iteration will be:
cout << anArray[-1] << endl;

The first one prints the first element and the second one is clearly an error. What you want to do is start looper as the last element of the array, which is equal to size-1, so the for-loop should turn into:
for(looper=size-1;looper>=0;looper--)

As well, most people use int i; instead of looper. It's just easier to write. cout << anArray[i]; Much easier, see?
Last edited on
closed account (D80DSL3A)
Your for statement on line 98 is set up to start at the wrong value. Since you're going backwards you want to start at the end of the array (index = size-1 ) and go to the beginning (index=0).
Try this instead:for (looper =size-1;looper >=0;looper--)

EDIT: I see wasabi beat me to it (and explained it more fully).
Last edited on
Also, your
void printBackwards ( int alpha_List, int beta_List ) { ... }
function definition doesn't match your
void printBackwards (const int anArray [], int size);
function header, and it follows that the anArray and size variables in your function definition have not been initialized yet.
Topic archived. No new replies allowed.