# Whats wrong with this code? inserting and sorting array..

Page 1 of 1

## 9 Replies - 702 Views - Last Post: 14 March 2012 - 01:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=269553&amp;s=0f744e1be3120a142fde3e26d7e55e23&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shifaza

Reputation: 0
• Posts: 23
• Joined: 08-March 11

# Whats wrong with this code? inserting and sorting array..

Posted 05 March 2012 - 02:34 PM

In this code im supposed to generate 100 random number between 1 to 1000 and insert into an array of 100 numbers. Also im supposed to sort the array. At first i generated the random number and then sorted in another function. But im told that im supposed to sort along the way.. like, find where the number fits and put it there.. and i dunno how..

This is what i came up with. And im getting an output which goes "999 999 999 999 ... " So clearly wrong.
Help?

#include "stdafx.h"
#include <iostream>
using namespace std;

int i = 0;
int arr[100] ={999};

int sortArray(int tempval)
{
int count = 0;
int arr[100] ={0};
int temp = 0;						//temp value to store whilst sorting

if(arr[i]>arr[i+1])
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
count++;
}

return 0;
}

void PrintArray(int arr[100])
{
for(int x=0; x<100; x++)				//displaying array after sorting
{
cout<<arr[i]<<"\t";
}

}

int main()
{
int tempval = 0;

for(int i=0; i<100; i++)				//100 numbers to be created
{
tempval = (rand() % 999)  +1;			//rand() function creating
sortArray(tempval);
i++;
}

cout<<endl<<endl<<"After sorting: "<<endl;
PrintArray(arr);

system("pause");
}

comments may not match wats done, coz i had to rewrite it again.

Is This A Good Question/Topic? 0

## Replies To: Whats wrong with this code? inserting and sorting array..

### #2 shifaza

Reputation: 0
• Posts: 23
• Joined: 08-March 11

## Re: Whats wrong with this code? inserting and sorting array..

Posted 05 March 2012 - 02:50 PM

One more thing... How can i make the counter work to see the number of comparisons made?

### #3 vividexstance

• D.I.C Lover

Reputation: 755
• Posts: 2,658
• Joined: 31-December 10

## Re: Whats wrong with this code? inserting and sorting array..

Posted 05 March 2012 - 03:02 PM

I don't know how this even compiled. When you want to use the rand() function, you need to include <cstdlib> and you should also include <ctime> as well. Now after that, you need to "seed" the random number generator before you can use it. Typically, people seed the random number generator with a call to time(0) which returns a unique number every time it's called and that's the number that is used to seed the random number generator:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
// Seed random number generator:
srand(time(0));

// Print out 10 random numbers between 1-10:
for(int i = 0; i < 10; ++i)
{
cout << "rand() = " << (rand() % 10) + 1 << endl;
}
}

Notice that a call like: rand() % 10 will produce a random number between 0 and 9 not 1 and 10, so just add a 1 to the result and you will have a number between 1 and 10.

### #4 shurd

Reputation: 37
• Posts: 162
• Joined: 05-February 12

## Re: Whats wrong with this code? inserting and sorting array..

Posted 05 March 2012 - 04:03 PM

Why are you using global variables? As well as creating the same array inside the sort function.

What you want to do is pass the array and the new number to your function, than look for a place where you can put it in. You can do that by a binary search or asking if the new number is bigger than the first index, if it is move the index to the right, if its not you shift your array to the right starting on the index you found and than put the new number there.

### #5 Martyr2

• Programming Theoretician

Reputation: 4818
• Posts: 13,082
• Joined: 18-April 07

## Re: Whats wrong with this code? inserting and sorting array..

Posted 05 March 2012 - 04:09 PM

As the others have eluded to, your sortArray function takes in an integer (tempval) but then it never even uses it. Instead you initialize an array (the same as the global one) and set all its elements to zero. Then you try to sort it? You are sorting zeros.

Look at the mechanics of your sortArray function first. It should be taking an array and the number of elements (like shurd said) and sort that. Not just an integer. Your main() function should be then passing the array to the function.

int sortArray(int arr[], int size) {

}

The others are right on with what they said.

P.S. This function can return an integer which will represent the number of swaps. To answer your question there, initialize a variable to 0 and each time you make your swap, increment it. Return that value at the end.

This post has been edited by Martyr2: 05 March 2012 - 04:11 PM

### #6 shifaza

Reputation: 0
• Posts: 23
• Joined: 08-March 11

## Re: Whats wrong with this code? inserting and sorting array..

Posted 05 March 2012 - 07:42 PM

Ok,

But, if i modify the main as:
int arr[100] ={999};
int count = 0;

for(int i=0; i<100; i++)				//100 numbers to be created
{
sortArray(arr, count) = (rand() % 999)  +1;			//rand() function creating
i++;
}

And function call as :
int sortArray(int arr[999], int count)

i see an error which says 'expression must be a modifiable lvalue'
why?

### #7 jdavi134

Reputation: 42
• Posts: 225
• Joined: 26-October 11

## Re: Whats wrong with this code? inserting and sorting array..

Posted 05 March 2012 - 09:13 PM

That means exactly what it says, you cannot call a function and then set the called function equal to something. That it is un-modifiable is completely correct.

Therefore: This line is incorrect
sortArray(arr, count) = (rand() % 999)  +1;

Jack

### #8 shifaza

Reputation: 0
• Posts: 23
• Joined: 08-March 11

## Re: Whats wrong with this code? inserting and sorting array..

Posted 14 March 2012 - 07:46 AM

Ok, so.. how do i correct it?

[late reply coz i wasnt feeling well]

### #9 vividexstance

• D.I.C Lover

Reputation: 755
• Posts: 2,658
• Joined: 31-December 10

## Re: Whats wrong with this code? inserting and sorting array..

Posted 14 March 2012 - 10:46 AM

jdavi134, on 05 March 2012 - 11:13 PM, said:

That means exactly what it says, you cannot call a function and then set the called function equal to something. That it is un-modifiable is completely correct.

Therefore: This line is incorrect
sortArray(arr, count) = (rand() % 999)  +1;

Jack

For the most part this is correct but if that function did return a modifiable lvalue like a pointer or reference to some object, then you can assign to a call function (ONLY in this situation though). It it returns a pointer, then of course you would have to dereference it first before you assigned to it.

### #10 shurd

Reputation: 37
• Posts: 162
• Joined: 05-February 12

## Re: Whats wrong with this code? inserting and sorting array..

Posted 14 March 2012 - 01:24 PM

To correct it you can assign your rand number to count, and than just call your function.
count = (rand()%999)+1;
sortArray(arr,count);
//And you can repeat that with a loop for how long you need.