Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 4th, 2006, 6:03 PM   #11
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
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";
might be easier to work with than
printf("Your score is %d out of a possible %d points.\n", score, total);
This is especially true when you have a lot of variables. With printf(), they are identified in the output not with their names, but with placeholders that must correspond to additional parameters you pass (this is why printf() can fail so badly if you don't pass in the correct number and/or type of arguments, and it's also why the compiler can't error-check the additional arguments).
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Dec 4th, 2006, 6:28 PM   #12
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by lectricpharaoh View Post
That said, you might find it easier using C++ constructs and functions rather than C ones.
That wouldn't be C, though :p
Arevos is offline   Reply With Quote
Old Dec 5th, 2006, 10:27 AM   #13
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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.
m0rb1d is offline   Reply With Quote
Old Dec 5th, 2006, 11:06 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 5th, 2006, 11:41 AM   #15
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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.
m0rb1d is offline   Reply With Quote
Old Dec 5th, 2006, 11:52 AM   #16
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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;
}
>What I dont understand, is why the previous will not work.
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;
}
You can shorten that with other C features, but I'm not sure you've encountered them yet:
n = 0;

do {
  myArray[n] = n;
} while ( myArray[n++] < 5 );
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Dec 5th, 2006, 8:00 PM   #17
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
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.
Harakim is offline   Reply With Quote
Old Dec 5th, 2006, 10:46 PM   #18
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Arevos
That wouldn't be C, though :p
My bad. I didn't notice the forum. In my defense, though, the OP mentioned he was using DevC++, and that he was finding C difficult; I don't feel a recommendation to start with an easier-to-grasp approach is entirely unwarranted. :/
Quote:
Originally Posted by Harakim
Also, notice that he used {} (braces).
What is it with everyone assuming Narue is a guy?
__________________
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.
lectricpharaoh is offline   Reply With Quote
Old Dec 5th, 2006, 11:14 PM   #19
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Sorry.
Harakim is offline   Reply With Quote
Old Dec 6th, 2006, 1:27 AM   #20
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC