Pointers, Classes, and Sorted Arrays Project
Jun 19, 2012 at 7:54pm UTC
I'm building a program that is supposed to take an array, initially empty, and insert user input numbers. Also, after the user inputs a negative number, the numbers that follow are to be removed. The catch is that the array must be sorted in ascending order the whole time and print the array after every insertion or removal. As of now, the program just outputs -33686019 0 alternatively. I don't know why the zero is even there because nowhere do I set any variable to that value. I'm also unsure if my overloaded operators are correct as I do not get that concept. An explanation of this would be very helpful. What's commented out is my first attempt at creating the insert/remove function, respectively.
With the user input as:1 3 2 7 5 9 7 -1 5 7 3 -1
The output should be:
1
1 3
1 2 3
1 2 3 7
1 2 3 5 7
1 2 3 5 7 9
1 2 3 5 7 7 9
1 2 3 5 6 7 7 9
1 2 3 6 7 7 9
1 2 3 6 7 9
1 2 6 7 9
2 6 7 9
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#include <iomanip>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
class SortedArray{
private :
int len;
int *n;
public :
SortedArray();
void insert(int );
void remove(int );
void print();
void sort(int *, int );
SortedArray operator +(int );
SortedArray operator -(int );
};
SortedArray::SortedArray(void ){
n=new int [1];
n[0]='\0' ;
len=1;
}
void SortedArray::sort(int *a, int b){
bool sorted;
int i=0;
int temp;
do {
sorted=true ;
for (i=0; i<b; i++){
if (a[i]>a[i+1]){
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
sorted=false ;
}
}
}while (!sorted);
}
void SortedArray::insert(int num){
len++;
int *tmp=n;
n=new int [len];
for (int i=0; i<len-1; i++) n[i]=tmp[i];
n[len-2]=num;
n[len-1]='\0' ;
sort(n, len);
}
/*int l=len+1;
int *newArray=new int[l];
for(int i=0; i<l-1; i++) {
newArray[i]=n[i];
}
newArray[l-1]='\0';
newArray[l-2]=num;
delete []n;
int *n2=new int[l];
for(int i=0; i<=l-1; i++) {
n2[i]=newArray[i];
}
n=n2;
delete []newArray;
sort(n, l);
len++;*/
void SortedArray::remove(int num) {
len--;
int *tmp=n;
n=new int [len];
for (int i=0; i<len-1; i++){
if (tmp[i]==num) tmp[i]=99;
}
for (int k=0; k<len; k++) n[k]=tmp[k];
sort(n, len);
n[len-1]='\0' ;
}
/*int *newArray;
int l=len-1;
for(int i=0; i<len; i++) {
if(n[i]=num) {
n[i]=50;
int *newArray=new int[l];
for(int j=0; j<len-1; j++) {
newArray[j]=n[j];
}
sort(newArray, l);
newArray[l-1]='\0';
delete []n;
int *n2=new int[l];
for (int k=0; k<l; k++) {
n2[k]=newArray[k];
}
n=n2;
delete []newArray;
len--;
}
}*/
void SortedArray::print(){
for (int i=0; i<len; i++) {
cout << n[i] <<" " ;
}
cout<<endl;
}
SortedArray SortedArray::operator +(int num) {
SortedArray z;
z.insert(num);
return z;
}
SortedArray SortedArray::operator -(int num) {
SortedArray y;
y.remove(num);
return y;
}
int main(){
SortedArray x;
int inp;
cin >> inp;
do {
x.insert(inp);
x.print();
cin >> inp;
}while (inp >= 0);
x+6;
x.print();
do {
cin >> inp;
x.remove(inp);
x.print();
}while (inp>=0);
x-1;
x.print();
system ("PAUSE" );
return 0;
}
Jun 20, 2012 at 6:04pm UTC
Please someone help!!!
Topic archived. No new replies allowed.