-
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.
:confused:
-
-
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
-
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();
}
-
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?
-
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....
-
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...
-
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......
-
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?
-
thx guys it's working correctly now.........thx for ur help....
-
#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.........?
-
#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.........?
-
<< !! 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.