Thread: array problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Code:
    #include<stdio.h>
    #include<conio.h>
      void main(){
    	int x[10],i,square=0;
    	clrscr();
    	for(i=0;i<10;i++)
    	  square=x[i]*x[i];
    	printf("square=%d",square);
    	getch();
    }
    Now lets look at it closer ( btw u use code tags like this [ code ] to close it [ /code ] - of course without the spaces between [ and / and [ and c ///hope that makes sense)

    First of all never use void main use
    int main and return 0 at end of main

    Do not use clrscr see FAQ why it is a bad thing to use, there are more portable ways.+ i dont see any need of using that function here.

    Now look at this piece of code
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(){
     int x[10];
     int i;
     int square=0;
     for(i=0;i<10;i++){
      square=x[i]*x[i];
      printf("square=%d\n",square);
      }
      getch();
     return 0;
     }
    when u compile everything is still oke but when u run it you should get pretty strange outputs...


    HINT: i didnt define something, in other words there is something undefined in your code which causes all your poblems...

    Try and see if you can solve it now....

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Aaah forget ...

    your are not defining int x[10];
    at all...
    x[0] can be 1245445 or whatever it feels like at the moment...

    Do a board search on how to declare arrays,
    at least that way you will have done something on this one...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM