söndag 23 november 2014

Week 2 gameprogramming I

The second week of the course and I am starting to understand the C++ programming structure. I have a long way to go but this week I got one step closer to creating a game!

The focus on my side is to learn "for" loops and how to get functions outside main to work and in what order i should code the functions. I have also learned how to get random values and sort them, how to make a small random map and much more.

I have not done all the program exercises but I will get to it later this evening.

So a little bit of code then.
__________________________________________________________
#include <iostream>
#include <time.h>

// My random function below named random.

int random(int min, int max)
{
return min + (rand() % (max - min + 1)); //int min and int max will be used later
}

int main(int argv, char* argc[])
{

int array1[5];
int swap = -1;
int end = 4;
int count = 5;
srand((unsigned int)time(0));

for (int i = 0; i < 5; i++)
{
array1[i] = random(0, 100); // 0 = int min and 100 = int max.
std::cout << array1[i] << " "; // print out the array
}

std::cout << std::endl; // just some space :D
std::cout << std::endl;
std::cout << "sortera" << std::endl;
std::cout << std::endl;

// run the "for" loop until we have cheeked all the array values.
for (int counter = count - 1; counter > 0; counter--)
{
.
for (int i = 0; i < end; i++)
{
// look what value is bigger and swap them.
if (array1[i] > array1[i + 1])
{
swap = array1[i + 1];
array1[i + 1] = array1[i];
array1[i] = swap;
}
}

end--;
}

for (int i = 0; i < 5; i++)
{
std::cout << array1[i] << " "; // print out the array again but this time it is sorted.
}

std::cin.ignore(1024, '\n');
std::cin.get();
return 0;
}

Inga kommentarer:

Skicka en kommentar