What wrong with my code

Im in my first semester of computer and c++ is my first language so forgive me if I dont use correct terminology. Anyways when I run my code it does what i want it to do, but it also has extra number after the text I want to show.

#include <iostream>
#include <stdio.h>
using namespace std;
void bubbleSort(int *, int );
void bubbleSort(float *, float);
void bubbleSort(char *, char);
void printElements(int *,int );
void printElements(float *,float );
void printElements(char *,char );

int main (){
int size, i, num[100];
float num1[100];
char letter[100];



cout << "Enter how many interger you want:"<< endl;

cin >> size;

cout << "Enter that amount of numbers: " << endl;



for( i = 0; i <size; i++){

cin >> num[i];

bubbleSort(num , size);
}




size = 0;

cout << "How many decimals do you want: " << endl;

cin >> size;

cout << "Enter that amount"<<endl;



for( i = 0; i <size; i++){

cin >> num1[i];

bubbleSort(num1 ,size);
}

size= 0;

cout << "How many letter do you want: " << endl;

cin >> size ;

cout<<"Enter that amount:"<< endl;

for( i = 0; i <size; i++){

cin >> letter[i];

bubbleSort(letter,size);
}

printElements(num,size);
printElements(num1,size);
printElements(letter,size);

system("PAUSE");
return 0;
}

void bubbleSort(int *x, int length) {
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(x[i]>x[j])
{
int temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}

void bubbleSort(float *x, float length) {
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(x[i]>x[j])
{
float temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}

void bubbleSort(char *x, char length) {
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(x[i]>x[j])
{
float temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
void printElements(int *x,int length)
{
int i=0;
for(i=0;i<10;i++)
cout<<x[i]<<", ";
cout << endl;
}

void printElements(float *x,float length)
{
int i=0;
for(i=0;i<10;i++)
cout<<x[i]<<", ";
cout << endl;
}

void printElements(char *x,char length)
{
int i=0;
for(i=0;i<10;i++)
cout<<x[i]<<", ";
cout << endl;
}
Perhaps explain the objective of your project? I'm relatively new as well and have come to learn that, no matter how vast or little the amount of code is, always remember to document the objective and tasks of your program. By doing so, you don't have to decipher the code when you look at it 6 months from now and others don't have to do so as well.
closed account (DEUX92yv)
For ease of reading, please use code tags (at the right under Format:) and indent your code! Also, comments would be appreciated.

I don't know what you mean by an "extra number." Please post the program's output. Read the comments I've added (in green). They should be a start. I'll look some more into the code and try to see what you mean.

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
#include <iostream>
#include <stdio.h>
using namespace std;
void bubbleSort(int *, int );
void bubbleSort(float *, float);
void bubbleSort(char *, char);
void printElements(int *,int );
void printElements(float *,float );	// You declare "size" as an int in [code]main[/main], but seem to use it
void printElements(char *,char );	// as an int, a float, and a char.
					// While this may work in your compiler,
                                        // it won't always work, and I'd recommend changing the arguments
                                        // so that each printElements function's second argument is an int
                                        // Do the same with each bubbleSort

int main ()
{
	int size, i, num[100];
	float num1[100];
	char letter[100];

	cout << "Enter how many interger you want:"<< endl;
	cin >> size;

	cout << "Enter that amount of numbers: " << endl;

	for( i = 0; i <size; i++){
		cin >> num[i];
		bubbleSort(num , size);
	}


	size = 0; // Unnecessary

	cout << "How many decimals do you want: " << endl;
	cin >> size;

	cout << "Enter that amount"<<endl;

	for( i = 0; i <size; i++){
		cin >> num1[i];
		bubbleSort(num1 ,size);
	}


	size= 0; // Unnecessary

	cout << "How many letter do you want: " << endl;
	cin >> size ;

	cout<<"Enter that amount:"<< endl;

	for( i = 0; i <size; i++){
		cin >> letter[i];
		bubbleSort(letter,size);
	}

	printElements(num,size);
	printElements(num1,size);
	printElements(letter,size);

	system("PAUSE"); // Not sure why you need this
	return 0;
}

void bubbleSort(int *x, int length)
{
	int i,j;
	for(i=0;i<10;i++)
	{
		for(j=0;j<i;j++)
		{
			if(x[i]>x[j])
			{
				int temp=x[i];
				x[i]=x[j];
				x[j]=temp;
			}
		}
	}
}

void bubbleSort(float *x, float length)  // See above comment about "size"
{
	int i,j;
	for(i=0;i<10;i++)
	{
		for(j=0;j<i;j++)
		{
			if(x[i]>x[j])
			{
				float temp=x[i];
				x[i]=x[j];
				x[j]=temp;
			}
		}
	}
}

void bubbleSort(char *x, char length)  // See above comment about "size"
{
	int i,j;
	for(i=0;i<10;i++)
	{
		for(j=0;j<i;j++)
		{
			if(x[i]>x[j])
			{
				float temp=x[i];
				x[i]=x[j];
				x[j]=temp;
			}
		}
	}
}

void printElements(int *x,int length)
{
	int i=0;
	for(i=0;i<10;i++)
		cout<<x[i]<<", ";	// You can condense these two cout lines: cout << x[i] << ", " << endl;
		cout << endl;
}

// Keep in mind, if the goal was to print numbers to the screen one-per-line, your code is fine, but if you want
// to put them all on the same line, the above cout lines can NOT be condensed, and the "cout << endl;" line must
// be moved outside of (below) the for loop's closing bracket

void printElements(float *x,float length)
{
	int i=0;
	for(i=0;i<10;i++)
		cout<<x[i]<<", ";
		cout << endl;		// Same comments as above
}

void printElements(char *x,char length)
{
	int i=0;
	for(i=0;i<10;i++)
		cout<<x[i]<<", ";
		cout << endl;		// Same comments as above
} 


I'm impressed that you understand bubble sorting if this is truly your first experience with programming. See comments in your code, and post your output. I'll get back with why I think it's printing an extra number.

For professionalism's sake, you also may want to spell-check your "cout" statements.


EDIT: each printElements function is independent of the "size" variable. They only print out the first 10 elements of each array. To fix this, change the for loop's iteration line: instead of for(i = 0; i < 10; i++), it should probably read: for(i = 0; i < length; i++).

Your code is also difficult to decipher because of the lack of unique argument names. In general, give each function's arguments unique names - and create unique variables for each (i.e. don't use "size" for all of your functions). Hope this helps

EDIT 2.0: @toum: noted and removed. Thanks for clearing that up
Last edited on
@trojansdestroy: you are wrong about the missing brackets. When the instruction after a if/for/while is a one-liner, it's allowed to omit them.

The problem in the code is indeed the fact the loops always do 10 iterations no matter what the array length is.

I also see 2 stylistic problems:
- The sorting algorithm used in bubbleSort() is not the Bubble Sort algorithm. It's more of a variant of Insertion Sort.
- bubbleSort() should not be called until all the values have been input. The function call should be put outside the for loop (an easy way to do that is to remove the brackets).
Topic archived. No new replies allowed.