Thread: array problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    array problem

    hi
    guys ,iam stuck in this problem for so long now ,could u plz help me out....actually i don't know much about arrays in c language and how to work with them,but still i have written a program ,i don't know why it is not running properly.......
    this is about printing the squares of numbers from 1 to 10 in an array.
    i did like this:
    #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();
    }

    plz help me out to print the squares of series of 10 elements in
    an array from 1 to 10.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code Tags!!!!!
    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 Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Put yourself in position of the compiler. What value has x[i]??
    Ha, what value. You declared array of 10 elements with values?
    undefined *undefined=unexpected

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Perhaps you want to do this:
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i,square=0;
    clrscr();
    for(i=1;i<=10;i++){
    	square=i*i;
    	printf("square=%d\n",square);
    }
    getch();
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    so how will i do this same problem if i had to do that by printing the sum of all these squares of 1 to 10by using arrays,then?

  6. #6
    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....

  7. #7
    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...

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    the above code that u have written is printing the output of 100 anyway ,it's not printing as what i would have liked it to print that is for each no.there should be a corresponding square written next to it from 1 to 10.so what changes will i have to make when i had to do like this,in that case?plz reply......

  9. #9
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int i,x[10];
    for(i=0;i<10;i++){
    	x[i]=(i+1)*(i+1);
    	printf("square of %d is %d \n",i+1,x[i]);
    }
    getch();
    }
    Will this do?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    thx guys it's working correctly now.........thx for ur help....

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int i,x[10],total=0;
    for(i=1;i<=10;i++){
    x[i]=i*i;
    total=total+x[i];
    printf("square of %d is %d \n",i,x[i]);
    printf("\ntotal=%d\n",total);
    }
    getch();
    }
    i have wriiten this code for printing the sum of the squares of 10 numbers from 1 to 10 in an array......
    plz tell me guys is this a correct code.......?
    it will be a huge boost for me ,if u would tell me is this a correct program that i have written.........?
    Last edited by galmca; 10-04-2004 at 02:59 PM.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int i,x[10],total=0;
    for(i=1;i<=10;i++){
    x[i]=i*i;
    total=total+x[i];
    printf("square of %d is %d \n",i,x[i]);
    printf("\ntotal=%d\n",total);
    }
    getch();
    }
    i have wriiten this code for printing the sum of the squares of 10 numbers from 1 to 10 in an array......
    plz tell me guys is this a correct code.......?
    it will be a huge boost for me ,if u would tell me is this a correct program that i have written.........?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    << !! Posting Code? Read this First !! >>
    http://cboard.cprogramming.com/showthread.php?t=25765
    Now make sure you read it!!!
    And make sure you use preview post before submitting any more posts.

    Start a new thread with code inside code tags.
    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.

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