I am teaching myself the programming language of C++. I have some programs written or fragments of programs written that won't compile. If I were to post these wuld anybody be able to tell me what is incorrect with them?
Dang, I was hoping this would be a thread about someone looking to hire C++ programmers in Austin. I'll take a look if you post it. B
Sure, go ahead and post it. C++ is one of the languages that I'm pretty good at and comfortable with; I did more coding in that language than any other during my years at UT.
No I haven't fixed it myself it's just taking me a while to pick which programs I am having trouble with first. But if you all are so eager to help, could somebody please explain arrays and possibly show an example?
An array is a data structure of pre-declared length for holding a single type of data. ex: An array of integers 1,2,3,4,5- int arr[5]; arr[0] = 1; arr[1] = 2; arr[2] = 3; arr[3] = 4; arr[4] = 5; Very simple. Arrays are an important building block for better data structures like vectors as well. I think it might be possible to declare a size 0 array, then add items, but I am not sure.
Do you have any books? If you have Kernigan and Ritchey's Programming in C (sic?) it's a pretty concise explanation of C. If you are using C++ you don't use arrays. You want to use the STL containers instead.
Lil Pun, it sounds as if you are getting good feedback here, but I just wanted to point out that there are some really good boards out there that you can peruse. Rather than even posting your specific question you can get a quick overview of the same query posted over and over again. ie, I hang out at javaranch quite a bit. There has to be a comparable bbs for c++ programmers.
No. Either: 1) Use a vector some other list. or 2) Dynamically allocate the memory for the array on the heap once the size is determined.
Mudbug, Yeah, I figured you would have to do a memory allocation. Like I, and several other people, have said, don't use arrays if you don't have to. There are much better data structures in C++. If it is something very simple where you know the size of the array beforehand, like in my example above, go ahead. Otherwise, vectors are just like arrays only better because you can add and remove values from the "end".
Here is a shell of one of the programs which I am trying to write. It is a program using arrays to create a game of bridge with a deck of 52 cards. I wrote what I want the program to do in the sections I couldn't figure out. Here is the shell below: // Card Dealing. A deck of cards is a sequence of numbers 0,1,...,51 // with (number / 13) equaling 0, for diamonds,1 for clubs 2 for hearts, // and 3 for spades and the value of (number % 13) represents the respective // faces of the cards. i.e. Cards 0...12 are the two, three, four, ... ,ace // of diamonds, 26...38 are 13...25 are hearts,clubs and 39...51 are spades. // The program initializes, shuffles and the deals-out // the cards to four players. Cards are sorted for each player. #include <iostream> using std::cout; using std::endl; #include <ctime> #include <string> using std::string; void initialize(int []); void shuffle(int []); void sort(int [], int); void swap(int &, int &); void deal(int[]); // create a deck of cards, shuffle, sort each hand // and then display each of the four player's cards main() { int deck[52]; initialize(deck); shuffle(deck); sort(deck, 13); sort(&deck[13], 13); sort(&deck[26], 13); sort(&deck[39], 13); deal(deck); return (0); } // initialize the deck of cards so that card = i void initialize(int card[]) { // code to initialize the deck } // This routine is passes a pointer to a deck of cards. It randomly // selects a card from the unshuffled part of the deck, swaps this card // with the last card in the unshuffled part of the deck. This effectively // reduces the size of the unshuffled deck by one card. It continues until // the unshuffled deck has only one card left. When done, the deck of cards // in the calling routine has been randomely rearranged (i.e. shuffled). void shuffle(int card[]) { srand(time(NULL)); int random_position, last_position; for (last_position = 51; last_position > 0 ; --last_position) { // write code to select a position at random from position // (from postiont 0 to last_position) and then // swap the card at this position with the card in last_position } // end for } // Sort the elenents in an array of specified size in increasing order void sort(int array[], int size) { for (int last_position = size -1; last_position > 0 ; --last_position) { // write code to find the position of the largest card // (from position 0 to last_position) and then // swap the card at this position with the card in last_position } // end for } // This function uses addresses to exchange the values of two // integer variables in the calling routine. Note the use // of pointers for each varaible's address passed to the function. void swap(int &number1, int &number2) { // write code exchange the values of number1 and number2 } void deal(int card[]) { // deal each of the four hands, consisting of the shufffled card, sorted // for each individual player. // For each card in the deck use print_card(card); to print out the // face_value and the suit of that card. } // end deal // this routine will print out the face value and the suit for // each of the cards in a deck. card must be between 0 and 51. void print_card(const int card ) { string suit[4] = {"Clubs", "Diamonds", "Hearts", "Spades"}; string face_value[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}; cout << face_value[card % 13] << " of " << suit[card / 13] << endl; }
Here's your shuffle method. Also, your main should return an int and I think that you should avoid using "using" statements. If you have code that's millions of lines long and there are a lot of using statements then it can become confusing. for (last_position = 51; last_position > 0 ; --last_position) { int r = rand() % 52; int temp = card[ last_position ]; card[ last_position ] = card[r]; card[r] = temp; } // end for
My guess is that this is just an exercise using arrays. But this is definitely not object-oriented programming which I think is a bad thing.