-3

Question1:

I want to make below stuff in a loop. I want to make variable name a1,a2 ... to a10

In this each element equal to 1,2,3 to 10.
ie.. a1 = 1,a2 =2 ...a10 =10;

and i want to count 1 to 10 . Size of array gonna be 10 .

How to make this program in loop?

 #include <stdio.h>
 int main(){
long long n;
int count = 0;

for( i = 1; i <10 ; i++ )
{
    n /= 10;
    ++count;
}

printf("Number of digits: %d", count);

}

Upper code doesn't work for a1 ,a2 to a10 . How to make variable a + i ? that represent a1 a2 ..a10 euqal to to number a + i = i; ??

I try to embed a picture here, it says I can't use picture .

Here is link : i.imgur.com/viYuPUH.png

Kindly let me know, if question is not clear ?

=========== sorry , i dont know that only 1 question is allowed to ask, so i'm asking here.

Natalie
  • 3
  • 5
  • 3
    Yep, it's not clear: why do you want to create a load of variables if you could use an array? I also don't understand what does 'I want to make below stuff in a loop' mean. What's more, your code shows your efforts to count the digits of a number (which is represented by an uninitialized variable, BTW), no signs of array creation. – ForceBru Oct 07 '16 at 10:47
  • 1
    what array has size 10? and where are a1...a10. you just count to 9 and print it afterwards – Kami Kaze Oct 07 '16 at 10:49
  • 1
    what output do you want from the code? – Pirate Oct 07 '16 at 10:50
  • I want to print a1 =1 a2 =2 a3 =3 ... a10 = 10 How to make variable a1,a2,a3 , a4 that equal to numbers ? a +i = i ? i guess it does not make a1,a2 .. in such ways . Yes size 10. a1 to a10 so it's 10. I dont know how to code it . Thank you (btw when i press enter , it publish my post, I can't use enter in posting ?) – Natalie Oct 07 '16 at 10:53
  • you just want to print a1 =1 a2=2 ... or you want to create variable like a1=1, a2=2...??? – Pirate Oct 07 '16 at 10:55
  • @Pirate I want a1,a2,a3 ,a4 ... to a10 with equal values .. Kindly see i added one link in question, it's named as "Kindly click here to see output i want " kindly click on it. i guess you get it . – Natalie Oct 07 '16 at 11:05
  • http://stackoverflow.com/questions/3137671/declaring-and-initializing-arrays-in-c – Julien Lopez Oct 07 '16 at 11:14

2 Answers2

0

To print what you want is enough to use printf and a counter like:

#include <stdio.h>

int main(void)
{
    int i = 0;
    int count = 0;

    printf("Insert number of elements: ");
    if(scanf("%d", &count) == 1)
    {
        while (i++ < count)
        {
            printf("a%d=%d;\n", i, i );
        }

        printf("Number of digits: %d\n", count);
    }
    else
    {
        printf("Wrong input\n");
    }
}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • @JulienLopez Inited the wrong one. Too fast writing. ;) Thx – LPs Oct 07 '16 at 12:39
  • @Pirate OP didn't mention array... And my example works for whatever value until INT MAX. Did you really downvoted me for that? – LPs Oct 07 '16 at 13:04
  • the owner clearly mention that the size of array gonna be 10. I know that we dint need array and there are a lots of way to print that output. But what owner asking is main thing i guess. – Pirate Oct 07 '16 at 13:07
  • @JulienLopez i changed code only because you are saying that it is going out of bound. I changed again. Now you ok? – Pirate Oct 07 '16 at 13:16
-3

Try this,

#include <stdio.h>
int main(){
long n;
int count = 0;
int a[10],i;

for( i = 0; i <10 ; i++ )
{
    n /= 10;
    ++count;
    a[i]=count;
    printf("a[%d]= %d\n",i+1,a[i]);

}

printf("Number of digits: %d", count);
}

enter image description here

Update :

 #include <stdio.h>
    int main(){
    long n;
    int count = 0;
    int a[10],i;

    for( i = 0; i < 10 ; i++ )
    {
        n /= 10;
        ++count;
        a[i]=i+1;
        printf("a[%d]= %d\n",i+1,a[i]);

    }

    printf("Number of digits: %d", count);
    }
Pirate
  • 2,886
  • 4
  • 24
  • 42
  • Thank you, When i use for( i = 3; i <=10 ; i++ ) Why does it not start a[3] to a[10] and Number of digits: should show 8 .. I try to use in tutorialspoint.com/compile_c_online.php What am i missing? I upvoted answer, why s it showing it need 15 batch. thank you – Natalie Oct 07 '16 at 11:48
  • because you are assigning count to a[i]. if you don't want that. just assign the i to array. wait, i'll add that code also. And if it solves your problem please accept the answer. – Pirate Oct 07 '16 at 11:50
  • 1
    Totally UB. You are accessing out of bounds the array when `i == 10`.... – LPs Oct 07 '16 at 12:20
  • The problem is not about starting from 1, the problem is about accessing index 10 of an array of size 10. Accessing an array outside of its index range is undefined behavior. Though if you compile with gcc you'll get a stack smashing error at execution. In your update you define an array of size 11 and use only the 10 last elements... – Julien Lopez Oct 07 '16 at 13:07
  • but we can do that na? we can start array index 1 also. And if there are 10 element and we started from 1 then we can also access 10th index. – Pirate Oct 07 '16 at 13:09
  • 1
    Yes. You could also write `int count = (5 * 5 - 3 * 5 >> 3) - 1;` instead of `int count = 0;`. Doesn't mean you should. – Julien Lopez Oct 07 '16 at 13:14