hello there! I need some serious assistance, and I can't figure this out by myself. I am writing a program that is SUPPOSED to ask the user for the size of an array, and ask for number values until the size of the ray is met. then, it sorts the numbers according to size. this is supposed to be done with a bubble sort. Please help me correct my program to fix this! my grade is on the line in my c++ class. Thank you!
this is the code:
#include <iostream>
using namespace std;
#include <iomanip>
int main () {
int Counter=0;
int Number=0;
int Size;
int i=0;
int temp;
cout << "What is the size of the array? ";
cin >> Size;
int a[Size];
for (i=0;i<Size;i++)
{
cout<<endl<<"Element #"<<i<<"= ";
cin >> a[i];
}
for (i=1; i<Size;i++)
cout <<a[i]<<endl;
{
if
temp= a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
#include <iostream>
usingnamespace std;
#include <iomanip>
int main () {
int Counter=0;
int Number=0;
int Size;
int i=0;
int temp;
cout << "What is the size of the array? ";
cin >> Size;
int a[Size];
for (i=0;i<Size;i++)
{
cout<<endl<<"Element #"<<i<<"= ";
cin >> a[i];
}
for (i=1; i<Size;i++)
cout <<a[i]<<endl; // Do you mean for this to be here or within the brackets?
{
if // You just leave this conditional if here without any conditions
temp= a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
cout << a[i]<<endl;
system ("PAUSE");
return 0;
}
Your code is quite the mess right now. You need to document properly.
i'm not really sure.... and sorry about the indentation. i dont know wht the condition is supposed to actually BE for the if statement and i THINK the for loop is supposed to be in the brackets?
This is the bubble sort func... I hope you study it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// bubble sort that sorts from smallest to biggest
void bubble_sort(int array[], int sizeOfArray)
{
for(int iter = 1; iter < sizeOfArray; iter++)
{
for(int index = 0; index < sizeOfArray - iter; index++) // flipping the < changes it to sort to biggest to smallest
{
// basically, you want to go through the list and compare each and
// every one of the elements
if(array[index] > array[index+1]) // If the current element is bigger than the next element...
{
int temp = array[index]; // we create a temp variable and store the contents of the that element into this
array[index] = array[index+1]; // The next element becomes equal to the current element
array[index+1] = temp; // the next element is now equal to the previous element
}
}
}
}
my code was maybe a line from finished and because the dev compiler stopped working my teacher tried to fix it and deleted my program!! :( i don't know how to get back to where i was when it was near completion
so i rewrote the if statement like this
if (a[i] > a[i+1]) // You just leave this conditional if here without any conditions
{
temp= a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
and when i ran it, this happened
"
What is the size of the array? 4
if (a[i] > a[i+1])
{
temp= a[i];
a[i]=a[i+1];
a[i+1]=temp; // When it reaches the last element, it checks the next element.
// However, there is no next element. You are actually just accessing
// memory that should not be accessed.
}
Look at the code I have provided above and study it. iter prevents the the array from trying to access an index out of its bounds.