I need help..!

09/27/2012 18:40 jhaz88#1
I made a program for bubble sort, insertion sort, and selection sort...i made it using C...but our teacher want us to put it in C++ and use random # instead of fix...the problem is i don't know how to use C++..:( ..i try to do some self study about c++ but we need to finish this by sunday and i have no time...so any1 can help me pls pm me...i send the code..pls pls help me :(
09/27/2012 22:13 Dr. Coxxy#2
show your C-Code here, and we may give you some advice to port it to C++.
09/30/2012 17:00 Fawkess123#3
Just include "#include <iostream>" and now you can say its an c++ made program with some c functions ^^
09/30/2012 19:14 dasschild#4
Quote:
Originally Posted by Fawkess123 View Post
Just include "#include <iostream>" and now you can say its an c++ made program with some c functions ^^
Are you an Idiot? You should start reading about iostream. It's not a part of STL!

OT:
I assume he wants you to use vectors instead of arrays. A List could be also useful for this kind of task.
10/02/2012 01:41 jhaz88#5
For the bubble sort:
Code:
#include "stdio.h"
#include "conio.h"
void getmin (int list[],int *mv,int *mi, int size);
void sort(int list[],int size);
main()
{
    int x[20],min_value,min_index,size,i;
    printf("\nEnter the size you want: ");
       scanf("%d", &size);
          for(i=0;i<size;i++)
          {
           printf("\nIndex [%d]: ",i);
           scanf("%d",&x[i]);
           }
           printf("\nUnsorted:\n   ");
           for(i=0;i<size;i++)
           {
              printf("%d ",x[i]);
           }
    getmin(x,&min_value,&min_index,size);
    printf("\nMinimum:\n   %d at x[%d]\n",min_value,min_index);
    printf("Sorted:\n   ");
    sort(x,size);
       for (i=0;i<size;i++ )
		printf("%d ",x[i]);
}

void getmin (int list[],int *mv,int *mi,int size)
{   int i;
    *mv=list[0];
    *mi=0;
    for(i=1;i<size;i++)
    { if (list[i]<*mv)
      {  *mv=list[i];
          *mi=i;
      }
    }
}
void sort(int list[],int size )
{
	int small, j, temp ;
	for (small=0;small<=size-2; small++)
	{
		for (j=0;j<=size-2-small;j++ )
		{
			if (list[j]>list[j+1] )
			{   temp=list[j] ;
				list[j]=list[j+1] ;
				list[j+1]=temp ;
			}
		}
	}

}
For the Insertion sort :
Code:
#include "stdio.h"
#include "conio.h"
#define SIZE 7
void insertion(int list[],int size);
main()
{
  int x[7] = { 7,6,5,4,3,2,1} ,i;
           printf("Insertion Sort:\n");
           printf("\nUnsorted:\n   ");
           for(i=0;i<SIZE;i++)
           {
              printf("%d ",x[i]);
           }
    printf("\nSorted:\n   ");
   insertion(x,SIZE);
       for (i=0;i<SIZE;i++ )
		printf("%d ",x[i]);
		printf("\n");
}

void insertion(int list[],int size)
{   int i, j, k, temp,d ;
	for (i=1;i<=SIZE-1;i++)
	{	for (j=0;j<i;j++ )
		{
			if (list[j]>list[i] )
			{
				temp=list[j];
				list[j] = list[i];
				
				k=i;
	    	for (d=1;d<=i-j;d++)
	    	 {
               list[k]=list[k-1];
	    	    k--;
	    	
             }
	    	
			}
		}
	}

}
For the Selection Sort:
Code:
#include "stdio.h"
#include "conio.h"
void getmin (int list[],int *mv,int *mi, int size);
void sort(int list[],int size);
main()
{
    int x[20],min_value,min_index,size,i;
    printf("\nEnter the size you want: ");
       scanf("%d", &size);
          for(i=0;i<size;i++)
          {
           printf("\nIndex [%d]: ",i);
           scanf("%d",&x[i]);
           }
           printf("\nUnsorted:\n   ");
           for(i=0;i<size;i++)
           {
              printf("%d ",x[i]);
           }
    getmin(x,&min_value,&min_index,size);
    printf("\nMinimum:\n   %d at x[%d]\n",min_value,min_index);
    printf("Sorted:\n   ");
    sort(x,size);
       for (i=0;i<size;i++ )
		printf("%d ",x[i]);
}

void getmin (int list[],int *mv,int *mi,int size)
{   int i;
    *mv=list[0];
    *mi=0;
    for(i=1;i<size;i++)
    { if (list[i]<*mv)
      {  *mv=list[i];
          *mi=i;
      }
    }
}
void sort(int list[],int size)
{
  int a,b,c,temp;
   for(a=0; a<size; a++)
	{   c = a;
		for(b=a; b<size; b++)
		{
			if(list[c]>list[b])
			{
				c = b;
			}
		}
	    temp = list[a];
		list[a] = list[c];
		list[c] = temp;

	}

}