Hjälp med 2-dimensionell array i c++
Hej!
Jag ska göra ett program som slumpar fram tal i en 2-dimensionell array och sen sortera den med avseende på den första kolumnen i första hand.
Jag har 2 problem.
Det första är att när jag ska byta plats på två rader så blir det fel.
Min funktion ser ut så här:
void debug (arr2 array, int i)
{
cout << array[i][0] << "," << array[i][1] << " " << array[i+1][0] << "," << array[i+1][1] << endl;
}
void swap (arr2& array, int i)
{
int temp = array[i][0];
int temp2 = array[i][1];
debug(array, i);
array[i][0] = array[i+1][0];
debug(array, i);
array[i][1] = array[i+1][1];
debug(array, i);
array[i+1][0] = temp;
debug(array, i);
array[i+1][1] = temp2;
debug(array, i);
}
Debug-funktionen skriver ut de två raderna för att jag ska se vad som blir fel.
När jag testkör får jag följande:
16,5 5,4
5,5 5,4
5,4 4,4
5,16 16,4
5,16 16,5
Jag försöker alltså få 16,5 och 5,4 att byta plats men istället får jag 5,16 16,5.
Det första steget verkar fungera, men när jag ska sätta array[i][1] till array[i+1][1] så ändras även array[i+1][0] och array[i+1][1] av någon anledning.
Mitt andra problem är min randomiserings-funktion som ser ut så här:
void randomize(arr2& array)
{
srand ( time(NULL) );
for (int i = 0 ; i < 20 ; i++)
{
array[i][0] = rand() % 18 + 1;
array[i][1] = rand() % 8 + 1;
}
}
Jag vill att slumptalen i andra kolumnen ska gå från 1 till 8, men den går till 18 verkar det som.
Här är hela koden:
#include <iostream>
#include <cmath>
#include <cstring>
#include <time.h>
#include <cstdlib>
using namespace std;
typedef int arr[1];
typedef arr arr2[19];
void randomize(arr2& array)
{
srand ( time(NULL) );
for (int i = 0 ; i < 20 ; i++)
{
array[i][0] = rand() % 18 + 1;
array[i][1] = rand() % 8 + 1;
}
}
void print (arr2 array)
{
for (int i = 0 ; i < 20 ; i++)
{
cout << "Bana: " << array[i][0] << " Antal slag: " << array[i][1] << endl;
}
}
void debug (arr2 array, int i)
{
cout << array[i][0] << "," << array[i][1] << " " << array[i+1][0] << "," << array[i+1][1] << endl;
}
void swap (arr2& array, int i)
{
int temp = array[i][0];
int temp2 = array[i][1];
debug(array, i);
array[i][0] = array[i+1][0];
debug(array, i);
array[i][1] = array[i+1][1];
debug(array, i);
array[i+1][0] = temp;
debug(array, i);
array[i+1][1] = temp2;
debug(array, i);
}
void sort (arr2& array)
{
int j;
int temp;
int temp2;
int temp3;
int temp4;
bool ingafel = false;
while (ingafel == false)
{
ingafel = true;
j = 0;
for (int i = 0 ; i < 19 ; i++)
{
if (array[i][0] > array[i+1][0])
{
cout << endl;
cout << "Före :";
debug(array, i);
cout << "Byter plats på array[" << i << "][0] och array[" << i + 1 << "][0] för att " << array[i][0] << " > " << array[i+1][0] << endl;
swap(array, i);
cout << "Efter :";
debug(array, i);
ingafel = false;
}
else if (array[i][0] == array[i+1][0])
{
if (array[i][1] > array[i+1][1])
{
cout << endl;
cout << "Före :";
debug(array, i);
cout << "Byter plats på array[" << i << "][1] och array[" << i+1 << "][1] för att " << array[i][1] << " > " << array[i+1][1] << endl;
swap(array, i);
ingafel = false;
cout << "Efter: ";
debug(array, i);
}
}
}
}
}
int main ()
{
arr2 array;
randomize(array);
cout << "Osorterad:" << endl;
print(array);
sort(array);
cout << "Sorterad:" << endl;
print(array);
return 0;
}