Arrays

Hey, been working on this code for a while now, and it been having some troubling with it. It is supposed to take numbers from the user, sort them the report the results. But unfortunately, I cannot figure it out why it doesn't run. Hope someone can help, thanks

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
  #include <iostream>
using namespace std;

int getInput(int data[], int capacity, int sentinal);
void bubblesort(int data[], int length);
void printArray(int data[], int length);


int main()
{	int c,s, length;
	int data[100];	

	getInput(data, c, s);
	bubblesort(data,length);
	printArray(data,length);
	
	return 0;
}

int getInput(int data[], int capacity, int sentinal)
{
	int a, i=0;
	while(i < capacity)
		{
			cout << "Enter number:";
			cin >> a;
			
			if(a==-1)
			{
				return 0;
			}
			else
			{
				data[i]=a;
				i++;
			}
		}
	
/*sentinal= -1;
	int i=0, int a;
	cout<<"Enter the size of data";
	cin>>capacity;
	
	
	/*while(data[i] != -1)
	{
	for (i =0; i < capacity; i++)
	{	cout << "Enter item : ";
		cin >> data[i];
	}
}*/
}

void bubblesort(int a[], int length)
{ int swap;
	for(int i=1; i<length ; i++)
	{	for( int j=0; j<(length-i);j++)
		{	if( a[j] > a [j+1] ) 
			{	swap=a[j];
				a[j] = a[j+1];
				a[j+1]=swap;
			 } 
		}
	}
}

void printArray(int data[], int length)
{
	cout << "Enter values from greatest to least" << endl ;
	
	for(int i=0; i<length; i++)
	{
		cout << "The values are:";
	}
}
Last edited on
closed account (48T7M4Gy)
Well, what you are supposed to do is carry out 'unit testing'.

What this means here is because you didn't write your program all in one slab of code (unless you copied it off someone else ... bad) and then tried to run it, you should just test it in small units.

So go back to main and comment out all the lines of code and bring each back onto line only when the program runs for the preceding ones.

Do that and report where and what errors you are getting after you reach an insurmountable problem.
You're passing getInput() uninitialized variables (c and s) which are then used in the conditional on line 23. This causes undefined behavior.
closed account (48T7M4Gy)
This is a start on fixing up the first function.

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
#include <iostream>

using namespace std;

void getInput(int*, int&, int);
void bubblesort(int*, int);
void printArray(int [], int);

int main()
{
    int length = 100;
    int sentinel = -1;
    
    int data[100] ={0};
    
    getInput(data, length, sentinel);
    printArray(data, length);
    
    bubblesort(data, length);
    
    
    return 0;
}

void getInput(int* data, int& limit, int sentinel)
{
    int a = 0;
    int i = 0;
    
    while(i < limit)
    {
        cout << "Enter number:";
        cin >> a;
        
        if(a == sentinel)
        {
            limit = i;
            return;
        }
        else
        {
            data[i] = a;
            i++;
        }
    }
}

void bubblesort(int a[], int limit)
{
    int swap;
    for(int i=1; i< limit ; i++)
    {
        for( int j=0; j<(limit-i);j++)
        {	if( a[j] > a [j+1] )
        {	swap=a[j];
            a[j] = a[j+1];
            a[j+1]=swap;
        }
        }
    }
}

void printArray(int data[], int limit)
{
    for(int i = 0; i < limit; i++)
    {
        cout << "The values are:" << data[i] << endl;
    }
}
closed account (48T7M4Gy)
Another way of doing the same.

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
#include <iostream>

using namespace std;

int getInput(int[], int, int);
void bubblesort(int[], int);
void printArray(int[], int);

int main()
{
    int length = 100;
    int sentinel = -1;
    
    int data[100] ={0};
    
    length = getInput(data, length, sentinel);
    printArray(data, length);
    
    bubblesort(data, length);
    
    
    return 0;
}

int getInput(int data[], int limit, int sentinel)
{
    int a = 0;
    int i = 0;
    
    while(i < limit)
    {
        cout << "Enter number: ";
        cin >> a;
        
        if(a == sentinel)
        {
            return i;
        }
        else
        {
            data[i] = a;
            i++;
        }
    }
    
    return 0;
}

void bubblesort(int a[], int limit)
{
    int swap;
    for(int i=1; i< limit ; i++)
    {
        for( int j=0; j<(limit-i);j++)
        {	if( a[j] > a [j+1] )
        {	swap=a[j];
            a[j] = a[j+1];
            a[j+1]=swap;
        }
        }
    }
}

void printArray(int data[], int limit)
{
    for(int i = 0; i < limit; i++)
    {
        cout << "The values are: " << data[i] << endl;
    }
}
OK, that makes sense. But why aren't the values going through the bubble sort?
closed account (48T7M4Gy)
I know it makes sense. I reckon your teacher would have expected you to do a bit more than just assessing other people's work.

So, c'mon, it's your turn now.

What testing have you done? Maybe just print out some values inside the function as it goes through the loops. Show us what you get as output, modify it with some suggestions of your own and show us here.

PS Don't just throw the problem back to your boss to solve. It's not a good way to further a programming career. Just a suggestion. :)

(PS Past experience says OP won't be back, never to be heard of again, but you're welcome if a bit of effort is shown.)
Last edited on
closed account (48T7M4Gy)
Hint: It takes one line in what I posted. And that line doesn't even require modification.
When running it through the checks it is going through the bubblesort function, but it isn't switching the values . Ill show you the code edited...

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

#include <iostream>

using namespace std;

int getInput(int[], int, int);
void bubblesort(int[], int);
void printArray(int[], int);

int main()
{
    int length = 100;
    int sentinel = -1; // ending point
    
    int data[100] ={0};
    
    length = getInput(data, length, sentinel);
    bubblesort(data, length);
   	printArray(data, length); 
    
    return 0;
}

int getInput(int data[], int capacity, int sentinel)
{
    int a = 0, i = 0; // values start at 0
    
    while(i < capacity)
    {
        cout << "Enter number: ";
        cin >> a;
        
        if(a == sentinel)
        {
            return i;
        }
        else
        {
            data[i] = a;
            i++;
        }
    }
    
    return 0;
}

void bubblesort(int a[], int capacity)
{

    int swap;
    for(int i=0; i< capacity ; i++)
    
    {
    	cout << "Check" << endl; // checking value, going thru
        for( int j=0; j<(capacity-i);j++)
        {	
        	cout << "Check2" << endl;
			if( a[j] > a [j+1] )
	        {	
	        cout << "Check5" << endl;
				swap=a[j];
	            a[j] = a[j+1];
	            a[j+1]=swap;
            
        	}
        }
    }
}

void printArray(int data[], int capacity)
{
    for(int i = 0; i < capacity ; i++)
    {
        cout << "The values are: " << data[i] << endl;
    }
}


closed account (48T7M4Gy)
line 52 has a problem ... compare with corresponding OP line ... why the change?
Do you mean line 51? I changed the i=0 so the values will start from 0. What do you mean when you say OP line?
closed account (48T7M4Gy)
OP stands for original post.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/202169/
Topic archived. No new replies allowed.