Array elements values not being assigned

This is a discussion on Array elements values not being assigned within the C Programming forums, part of the General Programming Boards category; Hey all, I am new to C programming and I have written this small program to work out this maths ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Red face Array elements values not being assigned

    Hey all,

    I am new to C programming and I have written this small program to work out this maths problem our teacher gave to us (and yes she said i could write a prog to work it out) and i think i've got the right idea, except that in the 2 places in an if and else i have statements to assign values to array elements, the compiler gives warnings saying "Code has no affect" or somehting like that, and thus the element's aren't getting their values assigned. When i run the program it gives me a error window to either ignore or close.

    Any help shall be much appreciated.

    Heres the code:
    ===start code===

    #include <stdio.h>
    /* These are the libary function files to be included*/
    #include <stdlib.h>

    #define LOCKERS 1000
    /* Defines constants */
    #define STUDENTS 1000

    int loop_num;
    /*Defines variables*/

    int lockers[1002];
    /* Defines array*/

    void calc_locker_num(int student);
    /* Function prototypes*/

    void change_state(int num); /* another function*/

    int loop2; /* variable*/

    main()
    {
    puts("Welcome, this program will calculate the maths problem");
    /* writing a bit of info on screen*/
    puts("and then display the results on screen");
    getchar();
    /* more info*/

    /*loop to assign all array elements in lockers[] to 1 (open)*/
    for(loop2 = 0; lockers[loop2] != 1 && loop2 <= 1000; loop2++)
    {
    lockers[loop2] = 1;
    }

    /* cycles through the students and then sends info to function calc_locker_num()*/
    for(loop_num = 1; loop_num <= 1000; loop_num++)
    {
    calc_locker_num(loop_num);
    }
    return 0;
    }

    void calc_locker_num(int student)
    {
    int locker_loop = 1;
    for (; locker_loop % student == 0 && locker_loop <= 1000; locker_loop++)
    {
    change_state(locker_loop);
    }

    }


    void change_state(int num)
    {
    if ( lockers[num] == 1 )
    {
    lockers[num] == 2; /* compiler moaning about this one*/
    }
    else
    {
    lockers[num] == 1; /* and this one*/
    }
    printf("\n Locker number %d is open1 closed 2: %d " , num, lockers[num] );

    }

    ===End Code===

    please excuse if it is in bad style;P im a newbe.

    -SaRdIeN

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > lockers[num] == 2;

    Using == is a comparison (like for use in an if statement)..

    Use = for assignment, ie
    lockers[num] = 2;

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Smile thanks man:) that solves that prob!

    thanks man, see that just proves my inexperience:P
    i overlooked that double == and my brain saw it as = LoL.
    common mistake im hoping or im i really stupid

    thanks again

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Common beginner mistake - not you

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Post heheh ok:P

    ok fine i'll put it down as begginer's mistake:P

    its working now, and either my code is wrong, or my results are right:P

    i'll show my code and results to my maths teacher tomoz (yes shes also a programmer lol).

    thanks for help man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  3. copying values of an array to another array?!
    By webznz in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 10:59 AM
  4. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21