#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    81
    Rep Power
    1

    [Dev C++] 1D Array, Windows Error Found!


    Hey C++ Programmers...

    Yesterday i was learn an big part of lession 3, called data types, arrays and pointners...
    Now i test my C++ knowledge about what i have learned yesterday, called about arrays...

    Now i try to create an 1-dimensional array, with given an x value (or x = x + 1 / the same
    as x++ yet)... and this is my script, written in "Dev C++" on Windows XP (used an older
    computer yet)...:




    1D Array, Dev C++ - Windows XP:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int array[] = {1, 2, 4, 8, 16, 32, 64};
    	int x;
    	
    	array[x] = 124;
    	
    	for (x = 0; x < 124; x++)
    	{
    		cout << x << " = " << array[x] << endl;
    		x = x + 1;
    	} 
    	
    	return 0;
    }

    If i try to run this script, i get an windows error...., and note... this is not an compiler error, realy an windows error...
    It said thad there is an windows error occered in the name of my script...

    Whats wrong with my script ?.... what can be bad on it ?.... i use an old windows system, called
    Windows XP, however... some times...

    At home i get on my computer windows 10 and on my laptop still windows 7 (i think windows 7),
    but note... this script is written on windows xp yet.... is this it ?.... or get anyone any idea about
    whats wrong ?...

    Thanks for help ?.... Jamie.
    Last edited by JamieVanCadsand; May 3rd, 2018 at 03:01 AM.
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,744
    Rep Power
    9646
    A few problems here.

    1. Local variables like your x are not initialized to zero. They're undefined, meaning you can never really be sure what the value is when it's first declared. For all intents and purposes, x will have a random value.
    2. I don't know what you're trying to do with array[x]=124. Do you want to overwrite one of the existing values? That's okay. Do you want to append to the array? That's not okay. Are you trying to define its length? I don't know how but that would explain the next problem.
    3. Your loop continues until 124 but the array only has 7 values. That means any value of x>=7 (because the array indexes are 0-6) is a problem.
    4. You're incrementing x twice: once as part of the for loop (x++), once inside the loop (x=x+1). x starts at 0, then will be 2 in the next time through, then 4, and so on until it reaches or exceeds 124. (Since x will be even numbers it will reach 124 exactly.) This might be intentional for all I know.


    You're going too fast. You cannot rush learning C/C++ because you will get things wrong and it will cause problems. One step at a time. Learn one thing, learn it well, and then move on to the next subject.
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,967
    Rep Power
    509
    Turn on compiler warnings. The flag -Wall does this for my compiler. The compiler knows that x is uninitialized.
    Code:
    g++ -Wall C.C
    C.C: In function ‘int main()’:
    C.C:9:11: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
      array[x] = 124;
      ~~~~~~~~~^~~~~
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    81
    Rep Power
    1
    Originally Posted by requinix
    A few problems here.

    1. Local variables like your x are not initialized to zero. They're undefined, meaning you can never really be sure what the value is when it's first declared. For all intents and purposes, x will have a random value.
    2. I don't know what you're trying to do with array[x]=124. Do you want to overwrite one of the existing values? That's okay. Do you want to append to the array? That's not okay. Are you trying to define its length? I don't know how but that would explain the next problem.
    3. Your loop continues until 124 but the array only has 7 values. That means any value of x>=7 (because the array indexes are 0-6) is a problem.
    4. You're incrementing x twice: once as part of the for loop (x++), once inside the loop (x=x+1). x starts at 0, then will be 2 in the next time through, then 4, and so on until it reaches or exceeds 124. (Since x will be even numbers it will reach 124 exactly.) This might be intentional for all I know.


    You're going too fast. You cannot rush learning C/C++ because you will get things wrong and it will cause problems. One step at a time. Learn one thing, learn it well, and then move on to the next subject.
    OK... maby I must an little bit more time taking to learn C++... maby to
    do any lessions again... and sorry for my problem... now I am by classes
    but yet.... It is very fast yet... but maby I am too motivated to learn it...

    I Think, i must do some lessions again and even search for tutorials to learn C++ yet....
    I am too fast in my lessions, thats true yet...

    Thanks for our tip to take an little bit more time to learn C++, i try to take more times for
    each lession yet !.... i want to learn C++, just i take more time to it!...

    Thanks for our tip, Jamie.



    EDIT:

    It helps to take some more time to each lession to learn it beter... so now this is my array script,
    i learned from SoloLearn, here is my script about arrays:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int myArr[120];
    	int x;
    	
    	for (x = 0; x < 120; x++)
    	{
    		myArr[x] = 4;
    		cout << x << " " << myArr[x] << endl;
    	}
    	
    	return 0;
    }

    Thanks for our Tip..., it works!!... i hope thad this works...
    Jamie.
    Last edited by JamieVanCadsand; May 4th, 2018 at 12:23 PM.

IMN logo majestic logo threadwatch logo seochat tools logo