![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,007
Rep Power: 5
![]() |
Another linkie here might help. In fact, you can go through this site to see how to use a whole variety of functions.
That said, you might find it easier using C++ constructs and functions rather than C ones. Something like std::cout << "Your score is " << score << " out of a possible " << total << " points.\n"; printf("Your score is %d out of a possible %d points.\n", score, total);
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#12 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
|
|
|
|
|
|
#13 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
Learning C is turning out to be much more difficult than I originally had anticipated. Everything was going excellent, I was understanding the content, and could make my own code based on what I was learning. THEN, the page on arrays comes by.
http://computer.howstuffworks.com/c10.htm It's a little difficult to explain, but it's like, the comprehension is sitting right on the tip of my brain, and is refusing to make it's way to the understanding part of the brain. Very frustrating. I see the code, understand what it's supposed to do, and can BEGIN to see the HOW of it all, and THEN ZIP - I'm suddenly struck with stupidity. Sorry for the off topicness, but, needed to rant somewhere. |
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You haven't said what you don't understand, so let me just generalize. An array is a collection of contiguous memory locations that all hold the same type of value (ints, for example, or chars, or whatever). The array has a name. The name represents the address of the first location. The first location also has an index value of 0. Each succeeding element has an index. These increment from the beginning. You address these elements by combining the name of the array with an index value. MyArray [4] would be the fifth element in the array (since the first element is 0). Since MyArray (without an index attached) represents the first element, you could also access it by treating MyArray as a pointer and adding 4. If that's too confusing at the moment, stick to the index access method.
If this doesn't help, post specifically what is confusing to you.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
Well, to be more specific, I suppose assigning values to each number in the array. For instance:
You have myArray[5]. I understand that I can declare each one seperately, such as myArray[0] = 10 myArray[1] = 34 etc, but, doing it with a loop.... I dont quite understand. Goes back to that, tip of the brain comment I made earlier. It seems to me that the following should work: main()
{
int n;
n = 0;
int myArray[n];
WHILE ( myArray[n] < 5 )
myArray[n] = n
n = n + 1
}Reading the code, it appears to me that the WHILE loop will set myArray[n] to the value of n. It then sets n to n + 1, which, after the first loop, would be 1. So, the 2nd time through, it will make myArray[n] equal to 1, third time through it will be 2, and so on. Now, I am aware this will not work. It should be: WHILE ( n < 5 ) What I dont understand, is why the previous will not work. It seems like it's doing the same thing, only instead of setting the first array value ( myArray[0] ) to whatever, it setting the array variable n to whatever, which then sets myArray[0] to whatever n was. Seems like a lot of extra, un-needed work. |
|
|
|
|
|
#16 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
>Learning C is turning out to be much more difficult than I originally had anticipated.
People have some strange expectations, so let me drive the point home: Programming is hard. >It seems to me that the following should work: It should this way: int main ( void )
{
int n;
int myArray[5];
n = 0;
while ( n < 5 ) {
myArray[n] = n;
n = n + 1;
}
return 0;
}That's more subtle. When you declare the array, it's filled with garbage values by default. The only way to clear those values is to overwrite them. Now, when n is 0, you're still accessing myArray[0], but there's nothing there that you can predict. The problem is the order of operations. You use the contents of myArray[n] to control the loop, but you haven't assigned a value to myArray[n] yet. You can do what you want, but you have to be very careful about the order of operations. You need to assign to myArray[n] first, then test myArray[n], then increment n: int main ( void )
{
int n;
int myArray[5];
n = 0;
/* Infinite loop to keep the syntax simple */
while ( 1 ) {
myArray[n] = n;
if ( myArray[n] >= 5 )
break;
n = n + 1;
}
return 0;
}n = 0;
do {
myArray[n] = n;
} while ( myArray[n++] < 5 );
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#17 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
Also, notice that he used {} (braces). By default, a loop, switch/case statement, or if/else statement will only execute the next line of code. If you want to execute more lines of code, you will have to enclose it in braces.
|
|
|
|
|
|
#18 | ||
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,007
Rep Power: 5
![]() |
Quote:
Quote:
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick Last edited by lectricpharaoh; Dec 5th, 2006 at 10:59 PM. |
||
|
|
|
|
|
#19 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
Sorry.
|
|
|
|
|
|
#20 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
just some formatting crap. the data is the same with or w/o the "4".
it makes it print pretty. forget about it. some esoteric stdio nonsense with printf(). forget the "switch to python or ruby!!!" crap too. you made a good decision based on good advice. just keep doing what you're doing.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Confusion With Pointers and Arrays | JawaKing00 | C | 10 | Sep 14th, 2006 7:33 AM |
| dll confusion | NightShade01 | Visual Basic .NET | 3 | Aug 15th, 2006 11:47 PM |
| nested array confusion :S | chepfaust | Visual Basic | 5 | Mar 14th, 2005 2:09 PM |