Thread: Do it myself, but...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44

    Exclamation Do it myself, but...

    Hello:
    I am a SE student and I am just entering programming, an Intro to Logic and Design; very cool, I love it I am studying other elements of Computer Science on my own just to stay ahead of the mediocre pack: Javascript, HTML, C++, Encryption\Security (My goal is to program for the DOD, eventually)

    An assignment was given to us by our Prof to initialize a counter (in C) modified from a sum counter, originally; very basic, and seemingly easy. problem is, I am not yet very familiar with C format, functions, etc (this is not a C class, but the Prof is a C-programmer) I love jumping into the code, but honestly, I am lost: I compile and execute; MS-dos window opens and says "any key to continue" prompt.

    !I want no one to tell me what to do with this (or to write it for me--that's absurd!)because unless I figure out the majority of my problems in coding (as in life)myself I will learn nothing and that will come back to bite me one day when I am completing a very important Military app, etc.

    So, please just look at what I have: it is hopelessly sloppy and damned error-filled (I fear) and just tell me, if you would\ could that I am either wrong-right, cold-hot. I stayed up all night with this little thing and this morning I just started over.

    Thank-you, reRanger

    (20 integers counted\ by 5's)


    Code:
      #include <stdio.h>
    int main()
    {	
     int numbers[20]={5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100}
     int count=5;
     int count;
    	for(count=0; count<20; count++);
    
    		count=count+numbers[count];
    	   		
    	   
     printf("\n The count of all the numbers is %d",count);
    }
    Last edited by reRanger; 10-09-2004 at 12:06 PM.

  2. #2
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44

    Thumbs down Tried

    Tried to code tag_ Failed. Please excuse this

    reRanger

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code tags use brackets, not <>

  4. #4
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44

    Thumbs up ty

    Bithub, thank-you for your timely and helpful reply

    reRanger

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     int count=5;
    int count;
    You've got two variables called the exact same thing in the same scope. You should turn on your compiler's warnings if you aren't using them. It'll help find things like this.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    int count=5;
    int count;
    You probably want two different variables here

    > for(count=0; count<20; count++);
    Watch the ; at the end - this loop does nothing 20 times

    > printf("\n The count of all the numbers is %d",count);
    The \n usually goes at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I've re-written you code (not really changed) with comments. Hopefully the comments will give you a better idea of what is going on.

    Code:
    #include <stdio.h>
    int main()
    { 
      // Declare an array of 20 integers.  Initialize the array indexes to the values in the braces
      int numbers[20]={5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,8 5,90,95,100}
      // Declare an integer, and initialize it to the value of 5
      int count=5;
      // int count;  Ive commented out this line because we already have an integer named count
      // The following line does the following:
      // Initialize the variable count to 0.
      // while the value of count is less than 20, we will continue through this loop
      // after each iteration through the loop, add 1 to count.
      // As you can tell, this loop will execute 20 times.  
      for(count=0; count<20; count++)
      {
        // I've added the following line to show you how the count variable is incremented
        // in the loop.
        printf("The value of count is %d\n",count); 
      } // This closing brace indicate the end of the loop
    
      // This is an arithmetic operation.  Not sure what you are trying to do here, but basically
      // this is giving a new value to the variable count based upon it's current value.
      count=count+numbers[count];
    
      // The following line will print out the result of the above operation.
      printf("The count of all the numbers is %d\n",count);
    
      // we return 0 to indicate the program is finished.
      return 0;
    }

  8. #8
    random number generator reRanger's Avatar
    Join Date
    Oct 2004
    Posts
    44

    Arrow Bithub,quzah,salem:

    Bithub,quzah,salem:
    Thank-you all for your assistance; I have not implemented the suggestions yet as I am still reading over what y'all have shared with me. I must tell you, you have cleared up alot, I have learned a few very important things-- is that not the point? That's awesome. I just need a push in the right direction. I appreciate it guys.

    reRanger

Popular pages Recent additions subscribe to a feed