Sign in to follow this  
rpstaekwondo

c++ array help

Recommended Posts

rpstaekwondo    100
ok, i am learning c++ from a book, c++ for dummies, and i am at where you learn about arrays, but the book does a horrible job at explaining it, so what is an array, and how would i code one?

Share this post


Link to post
Share on other sites
daviangel    604
Get a better book since if you are new to C++ you should be learning to use vector and not array!
Just the fact that you are confused by arrays should tell you to avoid them unless necessary!

To net this out, arrays really are evil. You may not think so if you're new to C++. But after you write a big pile of code that uses arrays (especially if you make your code leak-proof and exception-safe), you'll learn — the hard way. Or you'll learn the easy way by believing those who've already done things like that. The choice is yours.


[Edited by - daviangel on March 1, 2008 7:17:51 PM]

Share this post


Link to post
Share on other sites
dave    2187
Quote:
Original post by daviangel
Get a better book since if you are new to C++ you should be learning about vector and not array!

To net this out, arrays really are evil. You may not think so if you're new to C++. But after you write a big pile of code that uses arrays (especially if you make your code leak-proof and exception-safe), you'll learn — the hard way. Or you'll learn the easy way by believing those who've already done things like that. The choice is yours.


No he shouldn't.

If he learns how to use a std::vector and never learns about arrays he will not learn why a vector is a good replacement for an array, what problems there are with arrays.

To the OP:

There are tonnes of links at google, arrays and std::vector. A little searching goes a long way.

Hope that helps,

Share this post


Link to post
Share on other sites
Darragh    308
Quote:
Original post by rpstaekwondo
ok, i am learning c++ from a book, c++ for dummies, and i am at where you learn about arrays, but the book does a horrible job at explaining it, so what is an array, and how would i code one?


Pretty simple really. An array is just a series of data elements stored one after the other in memory. You can put anything into arrays, numbers, characters etc..
An array containing 5 integers for example will just have five numbers stored in it, one after the other.

Here's how you would declare, access and modify that array in c++:



#include <cstdio>

int main()
{
// Declare an array containing five integers

int myArray[5];

// Set the value of each of the five numbers in the array, starting with the first

myArray[0] = 0;
myArray[1] = 5;
myArray[2] = 10;
myArray[3] = 15;
myArray[4] = 20;

// Print the value of each of the five numbers, starting with the first

printf( "1st number is: %i " , myArray[0] )
printf( "2nd number is: %i " , myArray[1] )
printf( "3rd number is: %i " , myArray[2] )
printf( "4th number is: %i " , myArray[3] )
printf( "5th number is: %i " , myArray[4] )

}






Quote:
Original post by rpstaekwondo

vector isnt even in the book....

what is a vector


A vector is a special C++ data structure that acts much like an array, but with additional benefits. You can resize vectors but you can't resize an array. You can also have checks put into your debug / test code with vectors to make sure you aren't trying to access invalid elements outside of the vector.

I'd stick to learning the basics (Arrays, pointers etc.) before worrying about things like Vectors though. Worry about that stuff later on when you have a leg up in the language!

[Edited by - Darragh on March 1, 2008 6:15:26 PM]

Share this post


Link to post
Share on other sites
rpstaekwondo    100
Quote:
Original post by Darragh
Quote:
Original post by rpstaekwondo
ok, i am learning c++ from a book, c++ for dummies, and i am at where you learn about arrays, but the book does a horrible job at explaining it, so what is an array, and how would i code one?


Pretty simple really. An array is just a series of data elements stored one after the other in memory. You can put anything into arrays, numbers, characters etc..
An array containing 5 integers for example will just have five numbers stored in it, one after the other.

Here's how you would declare, access and modify that array in c++:

*** Source Snippet Removed ***

Quote:
Original post by rpstaekwondo

vector isnt even in the book....

what is a vector


A vector is a special C++ data structure that acts much like an array, but with additional benefits. You can resize vectors but you can't resize an array. You can also have checks put into your debug / test code with vectors to make sure you aren't trying to access invalid elements outside of the vector.

I'd stick to learning the basics (Arrays, pointers etc.) before worrying about things like Vectors though. Worry about that stuff later on when you have a leg up in the language!


omg thank you so much, that was exactly the thing i was looking for, thank u

Share this post


Link to post
Share on other sites
shawnre    266
Well, I would assume that you know your basic data type (like int, double).

One way to store text in an array is to declare the array as type char, like the following:

char city[] = {'D','a','l','l','a','s'};

or as:

char city[] = "Dallas";

Edit: BTW, ask all the questions you want. So many questions from a beginner is not a bad thing. Just proves you want to learn! My .02

Share this post


Link to post
Share on other sites
pulpfist    528
The %i used with printf is called a format specifier

C++ inherit printf from the C language where printf was the common way to output text to the console. In C++ the preferred way is by using the cout object from the iostream library:

#include <iostream>

int main()
{
using namespace std;

cout << "1st number is " << myArray[0];
...
cout << "5th number is " << myArray[4] << endl;
}

Share this post


Link to post
Share on other sites
dashurc    236
I can understand the argument about USING vectors rather than arrays, but to suggest avoiding LEARNING about them is a seriously stupid idea. I don't ever want to work with someone who doesn't know what an array is, how to use them, or why it is often (but not always) better to use a more sophisticated structure like a vector.

Heck, it is often not an option to use the standard C++ containers since they have a tendency to bloat executable size which is an issue on consoles even if PC developers don't have to worry about it.

My suggestion, get a better book, learn arrays (at least their basics), learn vectors, use the one that applies for the situation.

Share this post


Link to post
Share on other sites
Shadow Wolf    184
I can't think of any good C++ starter books at the moment. However, the best way to find a book thats for you is to poke around on Amazon and read through the excerpts, or go to your local library or a Barnes & Nobel and spend some time looking through the books up there to determine which one speaks clearly to you and then get it. Oh, and you could also Google around for online guides and tutorials as well. :)

Share this post


Link to post
Share on other sites
daviangel    604
Quote:
Original post by rpstaekwondo
any book suggestions?

This book is recommended all the time around here and I also recommend it since it does prefer vectors to arrays:
Beginning C ++ Through Game Programming
Another option easy book that teaches C++ the way it's supposed to be used is "Accelerated C++".
If you want to learn C++ using legacy concepts like arrays,pointer based strings and the way you'll most likely see it in old code "C++ without fear" is your best bet. It spends several chapters recreating a string class even though most C++ compilers already come with a robust version!

Share this post


Link to post
Share on other sites
pulpfist    528
Quote:
Original post by rpstaekwondo
so the cstdio imports c code?


As I understand it yes. It remains part of the C++ standard libraries for backward compatibility with C.

Share this post


Link to post
Share on other sites
Chrono1081    108
Although I am a fan of the dummies series C++ for dummies is IMHO the worst out of the C++ beginners books. Terribly written and confusing.

Ok rant aside I REALLY like Beginnging Game Programming Through C++. I bought it already knowing most of the subjects in the book (actually all but vectors) and I LOVE it and still learned from it! Great book :D

Share this post


Link to post
Share on other sites
rpstaekwondo    100
Quote:
Original post by Chrono1081
Although I am a fan of the dummies series C++ for dummies is IMHO the worst out of the C++ beginners books. Terribly written and confusing.

Ok rant aside I REALLY like Beginnging Game Programming Through C++. I bought it already knowing most of the subjects in the book (actually all but vectors) and I LOVE it and still learned from it! Great book :D


is that one of those books that start you out by making a messagebox program and then you make a window and dont teach you the basics of the language?

i have a book like that and i didnt learn a thing because it was to complicated

Share this post


Link to post
Share on other sites
Chrono1081    108
Quote:
Original post by rpstaekwondo
Quote:
Original post by Chrono1081
Although I am a fan of the dummies series C++ for dummies is IMHO the worst out of the C++ beginners books. Terribly written and confusing.

Ok rant aside I REALLY like Beginnging Game Programming Through C++. I bought it already knowing most of the subjects in the book (actually all but vectors) and I LOVE it and still learned from it! Great book :D


is that one of those books that start you out by making a messagebox program and then you make a window and dont teach you the basics of the language?

i have a book like that and i didnt learn a thing because it was to complicated


No C++ through game programming is a great beginner book. It doesn't offer a ton of exercises but you can come up with those on your own. It does do a great job teaching. Id recommend you get it. (You will learn vectors in that book)

Share this post


Link to post
Share on other sites
rpstaekwondo    100
Quote:
Original post by Chrono1081
Quote:
Original post by rpstaekwondo
Quote:
Original post by Chrono1081
Although I am a fan of the dummies series C++ for dummies is IMHO the worst out of the C++ beginners books. Terribly written and confusing.

Ok rant aside I REALLY like Beginnging Game Programming Through C++. I bought it already knowing most of the subjects in the book (actually all but vectors) and I LOVE it and still learned from it! Great book :D


is that one of those books that start you out by making a messagebox program and then you make a window and dont teach you the basics of the language?

i have a book like that and i didnt learn a thing because it was to complicated


No C++ through game programming is a great beginner book. It doesn't offer a ton of exercises but you can come up with those on your own. It does do a great job teaching. Id recommend you get it. (You will learn vectors in that book)


is it game programming through c++ or c++ through game programming?

Share this post


Link to post
Share on other sites
shawnre    266
Introduction to Programming with C++ is the book we are using at my school for 100 level and 200 level classes. It is a fairly decent text, with tons of exercises at the end of chapters. It also has a companion website with online tests and such to check your knowledge of the material. The only problem would be how you would acquire the answers if this is not the instructors edition (doesnt really say on Amazon). That is, if you do some of the programming exercises, how would you know if you are right, or if there was a better way to do it, or if you just wanted to compare code with the author.

Hope that helps!

Share this post


Link to post
Share on other sites
dashurc    236
I'd recommend this book personally:

C++ How to Program

It doesn't have anything to do with game programming per se, but it teaches you pretty much all the basics properly, including pointers, arrays, and the STL (vectors, maps, etc.)

It's not a "For Dummies" book and won't try to explain everything in layman's terms (not that I've read any of the for dummies series), but it is a good beginner's text, and is even good to use as reference for experienced programmers.

Most of the other books recommended are probably just as good, but as I haven't personally read them, so I'm recommending something that I have read.

Share this post


Link to post
Share on other sites
neoajay    122
Hi Guys I dont know how to post a new problem
Here is my new Question plzzz help me in solving this.....

I am reading from a file Bigint.txt integers and storing in 2 arrays and trying to add them using operator overloading.

Here is my code.....
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
class stack
{
int arr[5];
public:
stack();
int top;
int arr1[5],arr2[5],temp1[10],temp2[5];
friend stack operator+(stack obj1,stack obj2);
}stack1,stack2,st3,temp;

stack::stack()
{
top=-1;
}
stack operator+(stack obj1,stack obj2)
{
stack temp;
for(int i=0;i<5;i++) {
temp.arr[i] = NULL;
}
for(i=0;i<5;i++) {
temp.arr[i] = obj1.arr1[i] + obj2.arr2[i];
}
return temp;
}
int main()
{

stack s1,temp2;
int i,j;
clrscr();
ifstream inFile;
inFile.open("Bigint.txt",ios::in);
if (!inFile)//||!inFile1)
{
cout << "Unable to open file";
exit(1);
}
else
{
for(i=0;i<10;i++)
{
if(i<5)
{
inFile>>s1.temp1[i];
s1.arr1[i]=s1.temp1[i];
}
if(i>=5)
{
inFile>>s1.temp1[i];
s1.arr2[i-5]=s1.temp1[i];
}
}
}
temp2=s1.arr1+s1.arr2; //-----> Here I'm getting error plz help...
for(i=0;i<5;i++)
{
cout << " "<<s1.arr1[i]<<" ";
}

for(i=0;i<5;i++)
{
cout << " "<<s1.arr2[i]<<" ";
}
inFile.close();
return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this