Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 23:12

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



I need help..!

Discussion on I need help..! within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #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
jhaz88 is offline  
Old 09/27/2012, 22:13   #2
 
Dr. Coxxy's Avatar
 
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++.
Dr. Coxxy is offline  
Old 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 ^^
Fawkess123 is offline  
Old 09/30/2012, 19:14   #4
 
elite*gold: 0
Join Date: Jun 2009
Posts: 132
Received Thanks: 37
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.
dasschild is offline  
Old 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;

	}

}
jhaz88 is offline  
Reply




All times are GMT +2. The time now is 23:12.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.