can you explain this

Apr 20, 2014 at 5:57pm
#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

void mysort(int vallist[]){

int i,j;
int temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}

for(i=0;i<10;i++)cout<<vallist[i]<<"\n";

}
int main(int argc, char *argv[])
{
int vallist[]={23,2,125,23,43,22,32,32,100,34};
mysort(vallist);
system("PAUSE");
return EXIT_SUCCESS; }


i confussed about this class:can anyone explaint it to me. line for line . thanks

void mysort(int vallist[]){

int i,j;
int temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}

for(i=0;i<10;i++)cout<<vallist[i]<<"\n";
Last edited on May 4, 2014 at 9:09pm
Apr 20, 2014 at 6:13pm
Define a function
bool isOdd (int num) {return num%2 == 1;}
Store all the inputted numbers into a container, e.g. std::list<int> numbers.
Apply this function to every element of the containter and place them into other containers e.g.
for (int x: numbers) {if (isOdd(x)) oddNumbers.push_back(x); else evenNumbers.push_back(x);}

Now display the elements of oddNumbers and evenNumbers.

You can also place into oddNumbers and remove at the same time, so that numbers becomes the evenNumbers set at the end (so only 2 sets are created in the end).

Another way, using only one set throughout, is to use std::partition:
auto iter = std::partition (numbers.begin(), numbers.end(), IsOdd);
Now cout the elements from numbers.begin() to (but not including) iter to get the odds, and from iter onwards to get the evens. However, the order of the elements may not be preserved because of all the swappings done in std::partition.


Last edited on Apr 20, 2014 at 6:26pm
Apr 20, 2014 at 9:36pm
thanks for the help. i just study all the things you've said....
Apr 21, 2014 at 3:34am
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
#include<iostream>

using namespace std;

void main()
{
	int num, x, evenArray[100], oddArray[20], evenCount=0, oddCount=0;
	cout<<"Enter numbers, enter \'0\' to stop: ";
	cin>> num;
	while(num!=0)
	{
		if (num%2==0)
		{
			evenArray[evenCount]=num;
			evenCount++;
		}
		else
		{
			oddArray[oddCount]=num;
			oddCount++;
		}
		cin>>num;
	}
	cout<<"Even numbers: ";
	for(x=0;x<evenCount;x++)
	{		
		cout<<evenArray[x]<<" ";
	}
	cout<<"\nOdd numbers: ";
	for(x=0;x<oddCount;x++)
	{		
		cout<<oddArray[x]<<" ";
	}
	cout<<endl<<endl;

}
Apr 21, 2014 at 3:42am
Yikes, you don't need so many arrays for all of this.
You can just do two passes over the array containing the input:
1
2
3
4
5
6
7
8
9
10
11
int array[8];
std::cout << "Enter 8 numbers: ";
for (int i = 0; i < 8; ++i)
    std::cin >> array[i];

std::cout << "Odd numbers: ";
for (int i = 0; i < 8; ++i)
    if (array[i] % 2 == 1)
        std::cout << array[i] << ' ';

// Similarly for even numbers 
Apr 21, 2014 at 4:33am
(y) that too @long double main
Thanks for the pointers
Last edited on Apr 21, 2014 at 4:33am
May 4, 2014 at 9:13pm
#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

void mysort(int vallist[]){

int i,j;
int temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}

for(i=0;i<10;i++)cout<<vallist[i]<<"\n";

}
int main(int argc, char *argv[])
{
int vallist[]={23,2,125,23,43,22,32,32,100,34};
mysort(vallist);
system("PAUSE");
return EXIT_SUCCESS; }


i confussed about this class:can anyone explaint it to me. line for line . thanks

void mysort(int vallist[]){

int i,j;
int temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}

for(i=0;i<10;i++)cout<<vallist[i]<<"\n";
Topic archived. No new replies allowed.