|
You last visited: Today at 23:12
Advertisement
I need help..!
Discussion on I need help..! within the C/C++ forum part of the Coders Den category.
09/27/2012, 18:40
|
#1
|
elite*gold: 0
Join Date: Oct 2009
Posts: 162
Received Thanks: 12
|
I need help..!
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
|
#2
|
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
|
show your C-Code here, and we may give you some advice to port it to C++.
|
|
|
09/30/2012, 17:00
|
#3
|
elite*gold: 0
Join Date: Dec 2011
Posts: 5
Received Thanks: 1
|
Just include "#include <iostream>" and now you can say its an c++ made program with some c functions ^^
|
|
|
09/30/2012, 19:14
|
#4
|
elite*gold: 0
Join Date: Jun 2009
Posts: 132
Received Thanks: 37
|
Quote:
Originally Posted by Fawkess123
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
|
#5
|
elite*gold: 0
Join Date: Oct 2009
Posts: 162
Received Thanks: 12
|
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;
}
}
|
|
|
All times are GMT +2. The time now is 23:12.
|
|