arrays

hey lads,

I was trying to write a code which will create an array with random integer values, and the number of elements in array should be inserted by the user. And I've got some conditions:

1. All the elements from the original array should be ascending.


2. Create a new array which will copy all the elements from the original array, following the condition:
In the second array first half of it copy all the odd elements and into the second half all the even elements.


3. Count how many permutations were accomplished to order all the odd elements and how many permutations were completed to order all the even elements.

4. User should input a number, which will identify the maximum values.( for example we have an array consisting of :

3 18 1 20 20 17 17

if user input number 4 it will provide the first 4 maximum values of this array, which are : 20 20 18 17. I hope you got my idea.

That's what I've done so far.

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
   #include <iostream>
  #include <cstdlib> 
  #include <ctime>
  #include <conio.h>
  #include <iomanip>
  
  using namespace std;
 
   
  int main()
  {
        srand(time(0));
       
        int na;
               
      cout<<"Enter A -> n =";
      cin>>na; // user input the number of elements for the array

      int a[1000]; // i presumed that the max number of elements could be 1000
      
      for(int i=0; i<na; i++)
      {
          a[i]=rand()%100;   // arrray a[] will have random values from 0-100
      }
      

// the code to order ascending the elements in the array. 

    int k=0;
    bool x=true;
    
    while(x)
    {
        
      x=false;
          
        for( int i=0,temp; i < na-1; i++)
        {
          if( a[i] > a[i+1] )
          {

          temp = a[i];
          a[i] = a[i+1];
          a[i+1] = temp;
          x=true;
          }
          
         } 
        
     }
     cout<<endl;
     cout<<endl;
     
      for(int i=0; i<na; i++)
      {
          cout<<setw(4)<<a[i];
      }
      
// create a new array which will save in its first half all the odd elements from the original array and in its second half all the even ones.
//Anyway smth. is not working properly and I don;t have a clue. Help :) 

    int v[1000],p=0;
      
    for(int i=0; i<na; i++){
    	p++;
           if (a[i]%2==0 && p<na/2){
           	
	       a[i] = v[p];
           
		   }
	  else if (a[i]%2!=0 && p>na/2){
	p++;
	       a[i] = v[p];
           
		   }
        }
        
    cout<<endl;
    cout<<endl;
       
  for (int i = 0; i < na; i++) {
    cout << setw(3) << v[p];
  }
  
// user input a number which will provide all the "x" maximum elements from the array. 
//Please advice: 

  cout<<" Enter x = "<<endl;
  cin>>x;
  
  for (int i = 0; i <= x; i++) {
      if (max < v[i]) {
      max = v[i];   
    }
  }
  cout <<" Maximum number of elements are  "<<max<<endl;


      return 0;
  }


//Thanks for your help guys,
Last edited on
you are overthinking it. just do 2 loops.
p = 0;
for(i = 0; i < na; i++)
if(a[i]%2)
v[p++]= a[i] ;
for(i = 0; i < na; i++)
if(a[i]%2 ==0)
v[p++]= a[i] ;

note the issue is that 'half' confused you, NA/2 is not right.
what if you have in your array 1,3,5,8,10,20,30,40,50 ?
half of that isnt quite right, because there are less odds than evens, and you did nothing to force there to be 1 to 1 distribution. So looping twice is a simple way to let them move to the right place naturally, without trying to figure out how many and where to split etc.
Last edited on
@ jonnin, thanks mate,
what's your opinion about 3 and 4 conditions?
condition Nr 4 is in the code above but I am not sure it will work fine and with Nr 3 condition I am stuck , i got the idea i need a counting variable but where do i place it and why
nr? conditions? Not sure what you are talking about.
if you are asking how to do 3&4, ... think them thru, give it a try.
@jonnin, i meant task 3 and 4, need help with them, here's what i've done so far;

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
      
    int v[1000],p=0;
    int evenfor=0, oddfor=0;
    
    for(int i = 0; i < na; i++)
       if(a[i]%2==0)
       v[p++]= a[i] ;
       if( p++ )evenfor++; // not sure why it doesn't really count the number of permutations

for(int i = 0; i < na; i++)
if(a[i]%2!=0)
v[p++]= a[i] ;
if(p++)  oddfor++;      // // not sure why it doesn't really count the number of permutations
    
    cout<<endl;
    cout<<endl;
    
    
  for (int i = 0; i < na; i++) {
    cout << setw(3) << v[p];
  }
  cout<<endl;
  
  cout<<" Enter x = "<<endl;
  cin>>x;
  
  int max=a[0];
  
  for (int i = 0; i < na; i++) {  // shows just the maximum value from the entire array, but i 
                                             //   need an "x" number of maximum values , where x is defined 
                                             //   by the user 
      if (max < a[i]) {
      max = a[i];
      
    }
  }
  cout <<" Maximum number of elements are  "<<max<<endl;
  cout <<" Even for= "<<evenfor<<" Odd for= "<<oddfor<<endl;
      return 0;
  }
Last edited on
it asked you to count the ordering (sort). You counted the copy.... (you need to count task 1. I would do this by having a counter and increment every time you swap something.

4) the easy way to do 4 is to copy the result of the sort and then just peel off the highest N values in a loop.
Last edited on
Topic archived. No new replies allowed.