Thread: Beginner in C programming (arrays)

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    23

    Beginner in C programming (arrays)

    Hey guys i am new to C programming and im trying to learn how to display elements within arrays, and heres an example of the code.

    Code:
    #include<stdio.h>
    
    int main() {
    
      int numInts;
      int i;
      int intArray[500];
      
      printf("Please enter amount of integers you want to generate (min is 5 and max is 100)\n");
      scanf("%d", &numInts);
    
      if ((numInts >= 5) && (numInts <= 100))  {
    
        /* generate numInts numbers */
        for (i = 0; i < 10000000; i++) {
          intArray[100] = i;
        }
    
        /* Write a statement that prints the 2nd element of the numInts array here. */
        printf("The second element is %d \n", intarray[100] );
    
        /* Write code to print out all the generated numbers here. */
    
        /* Write code to print out the first ten generated numbers here. */
    
         /* Write code to print out every second generated number here. */
    
         /* Write code that prints all generated numbers starting from the 5th number here. */
    
      }
      else {
    
        printf("Can not generate too many integers!!\n");
      }
      
      return 0;
    
    }
    I have managed to generate the second element however no matter how many integers i get the array to generate it always ends up in being 0. so how would i complete this program, thanks for your support.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (i = 0; i < 10000000; i++)
    Use the numInts value you read in as a loop limit

    > intArray[100] = i;
    100 should be something else, if you want to update more than a single element many times over.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    sorry i have no clue what you are saying,i might as well classify myself as a complete beginner.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Change your loop like the following.
    Your code,
    Code:
    for (i = 0; i < 10000000; i++) {
          intArray[100] = i;
        }
    Change it like the following.
    Code:
    for (i = 0; i < numInts; i++) {
          intArray[i] = i;
        }
    In your code you are assigning ,
    intArray[100]=i;
    Here your are assigning the value i to the same index 100.
    So value get replaced.
    Last edited by kiruthika; 03-22-2010 at 02:36 AM.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    oh ok then thank you, now i have followed your instruction, i am using the program called putty and i generate a "segmentation fault" error what does this mean??

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    probably that you are writing into an array index , ie, memory that you have not allocated, like if you declared int array[6] then tried to put something in array[7]

    if in your example you have not yet changed your loop to only count to the maximum number of elements you declared then it will definitely crash with seg fault

    edit:

    see comments >

    Code:
    int numInts;
      int i;
      int intArray[500];  //this says you have allocated 500 spaces to hold integers
      
      printf("Please enter amount of integers you want to generate (min is 5 and max is 100)\n");
      scanf("%d", &numInts);
    
      if ((numInts >= 5) && (numInts <= 100))  { //what is this for in relation the the array size you allocated?
    
        /* generate numInts numbers */
        for (i = 0; i < 10000000; i++) {  //this should be MAX_ELEMENTS' or something, previously declared for clarity and reusability
          intArray[100] = i;  //this as already explained means you are just going to copy over the contents of array element 100 repeatedly. 
        //you need to swap the '100' in the box for a counting variable, like the 'i' in your loop, 
        //that way it will access each element one after the other and write to them.
        //it will also crash with a seg fault if the number'i' is more than the number of elements you first declared.
        }
    remember array index numbering starts at array[0], ie the first element is not numbered [1], so in your example the 500th is numbered [499] remember this...
    Last edited by rogster001; 03-22-2010 at 07:03 AM. Reason: add example

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and i generate a "segmentation fault" error what does this mean??
    That you should post your latest code.
    There are way too many ways to screw up and get a segfault, we need to see code!.
    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.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    This program is supposed to ask for an integer off the user between 3 to 1000, then the array will generate that amount of numbers which has been asked by the user, so (numInts >= 3) && (numInts <= 1000) is a limiter if the user goes below 3 or greater than 1000 the program will error as listed in the else statement. Well he is the code i have just changed the numbers.

    Code:
    #include<stdio.h>
    
    int main() {
    
      int numInts;
      int i;
      int intArray[1000];
      
      printf("Please enter amount of integers you want to generate (min is 3 and max is 1000)\n");
      scanf("%d", &numInts);
    
      if ((numInts >= 3) && (numInts <= 1000))  {
    
        /* generate numInts numbers */
        for (i = 0; i < 10000; i++) {
          intArray[i] = i;
        }
    
        /* Write a statement that prints the 2nd element of the numInts array here. */
        printf("The second element is %d \n", intarray[1] );
    
        /* Write code to print out all the generated numbers here. */
    
        /* Write code to print out the first ten generated numbers here. */
    
         /* Write code to print out every second generated number here. */
    
         /* Write code that prints all generated numbers starting from the 5th number here. */
    
      }
      else {
    
        printf("Can not generate that many integers!!\n");
      }
      
      return 0;
    
    }
    running this will give me my segfault

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Code:
    /* generate numInts numbers */
        for (i = 0; i < 10000; i++) {
          intArray[i] = i;
        }
    it will fail becuase of this, i already tried to describe this to you

    you have allocated 1000 array spaces when you declared the array

    but in your loop you are counting to ten times that, which will crash it every time, you cannot write into that memory beyond number 999 (1000th element)

    maybe it is just a typing error, but that is why you should not use 'magic' numbers,

    declare
    Code:
    const int MAX_ELEMENTS = 1000;
    at the top of your program, then every time you mean '1000' you write MAX_ELEMENTS instead, this way you only have to change the figure in the const int declaration to modify every entry in the program, and there is little chance of typing errors messing things up like this
    Last edited by rogster001; 03-23-2010 at 04:21 AM.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    ahh silly me, thank you so much rogster001 for describing more clearly it helped a lot. one more question i do not know how to display the rest of the elements onto the screen i only know how to display one element at a time, i need help on the following, if its ok.

    Code:
     
        /* Write code to print out all the generated numbers here. */
    
        /* Write code to print out the first ten generated numbers here. */
    
         /* Write code to print out every second generated number here. */
    
         /* Write code that prints all generated numbers starting from the 5th number here. */
    with this i should be able to understand arrays more clearly and know how to call certain elements using specific code

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    greg, the remaining parts of the question are quite elementary, i am sure you have some study material to help you crack that bit, post your attempts if having problems
    once you have sorted out the first part as suggested it should be really easy for you to solve the remaining tasks. The only one that needs a little more logic is the print every other (every second generated) number, i would probably use an extra variable to control output there.

    by the way you realise that as it stands your numbers 'generated' are only going to be the index counter 'i' going from 0 to 999 right?
    no numbers are being randomly generated or anything, its just a counter
    also your user entering input does nothing more than control if the loop runs or not, it will always print all the numbers if input is valid at the moment so you need to change your for loop to count as the user requests "from val1 to val2"

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    i've managed to do the first three bits of code, however the last two we actually haven't learnt yet, i could try one way, but that would be way too time consuming, manually putting 500 intArray within a printf statement, but it would'nt be efficient

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try to think of what you need to do, in terms of everyday variable names.

    Code:
    int i, j, lowLimit, highLimit;
    
    lowLimit = 3;
    highLimit = 100;
    
    j = 0;
    for(i = lowLimit; i <= highLimit; i++) {
      YourArray[j] = i;
      ++j; 
    }
    Good variable names, clear & simple logic, and good indentation of your code, will go a long way to help you in programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Help with returning a string of arrays
    By yuliang11 in forum C Programming
    Replies: 8
    Last Post: 12-10-2005, 03:05 PM
  2. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  3. Beginner needs help with arrays.
    By justin69enoch in forum C Programming
    Replies: 7
    Last Post: 10-25-2002, 11:44 PM
  4. arrays and functions (beginner q)
    By eazhar in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2002, 05:39 AM
  5. Beginner needs help w/ran. nums. in arrays - URGENT!
    By madhouse199 in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2001, 07:12 AM