I'm trying to code a function that will make an array of consecutive numbers from 1
up to a number n
. For example, I would expect array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
. My code doesn't seem to be running but I don't exactly know why.
int count (int a)
{
scanf("%d", &a);
int i;
int array[a];
do
{
i = 0;
a = a - i;
array[a - 1] = a;
i++;
}
while (a > 0);
printf("Resulting array is %d", array[a]);
return 0;
}
You should use a for loop to create your array.
int i;
int array[a];
for(i = 0; i < a; i++) {
array[i] = i + 1;
}
return 0;
An idiomatic C way would be to define a function that fills an existing array:
void count(int *array, int max)
{
for (int i = 0; i < max; ++i)
{
array[i] = i+1;
}
}
You can call it eg like this:
int arr[10];
count(arr, 10);
for (int i = 0; i < 10; ++i)
{
printf("%d ", arr[i]);
}
Your do while
loop could simply be replaced by the following for
loop:
for (i = 0; i < a; i++)
array[i] = i + 1;
Similarly, for displaying each element in array
, you can iterate over the elements of array
by means of a for
loop:
for (i = 0; i < a; i++)
printf("Element at %2d is %d\n", i, array[i]);
First and foremost, the print statement
printf("Resulting array is %d", array[a]);
is wrong!! That's not how you print an array. You need to loop over the index, get the individual elements and print them.
Moreover, when you try using array[a]
, think of the value for a
. That's surely not what you want.
That said, there are other issues, like setting i
to 0
in each iteration of do...while
loop. That will lose the previously calculated value and start over. Move the initial assignment outside the loop body.
A slightly modified version, follow the comments
#include <stdio.h>
int count (void) // no need for parameter if you're asking user for size
{
int size = 0; //define size
int ret = scanf("%d", &size); //read it
if ( (ret != 1) || ( size < 1) ) return -1; //error check
int i = 0; //initialize local variables
int array[size]; //note: this is VLA
do
{
array[i] = i+1; //start populating elements
i++;
}
while (size > i); //go on unless you've reached the given size
puts ("Resulting array is"); // start printing ....
for (i = 0; i < size; i++)
{
printf("%d\t", array[i]); //...them one by one
}
return 0;
}
int main(void)
{
(void)count(); //call the function
}
Try something like this:
int* count(int a)
{
int array[a];
for (int i = 0; i < a; ++i)
{
array[i] = i + 1;
}
for (int i = 0; i < a; ++i)
{
printf("%d, ", i + 1);
}
return array;
}
Just use a simple "for" loop:
for (i = 0; i < a; i++)
{
array [i] = i + 1;
}
a
parameter. You can use a local do,while
is broken because you set i
to 0
at each iteration and its overly complicated. You can achieve the same result in a much easier way. An element at location i
has to have value i+1
, easy as it sounds. printf
prints only 1
element of the array. If you want them all, then you have to iterate over all of them. What about something like the following?
void count(){
int a,i=0;
scanf("%d", &a);
int array[a];
while(i<a){
array[i]=i+1;
i++
}
//now print it
i=0;
while(i<a)
printf(" %d ", array[i++]);
}
As an aside, note that you are using VLAs. That means there's a limit on the size of the array you can create which depends on your machine+compiler.
So there are a few bugs here . One that if you are accepting 'a' as a arg to function , why are you scanning it then. It just over writes the value you collected. Two the way you are trying to print the array is wrong as specified above and you should use a loop. Three , you can save a few lines like ,
for(int i =0; i<a ; i++)
{
arr[i]=i+1;
}
I'm trying to code a function that will make an array of consecutive numbers from 1 up to a number n.
In C++ there is already such a function that is named iota
and declared in the header <numeric>
.
In C such a function can look the following way
void iota(int a[], size_t n, int value)
{
for (size_t i = 0; i < n; i++) a[i] = value++;
}
Here is a demonstrative program
#include <stdio.h>
void iota(int a[], size_t n, int value)
{
for (size_t i = 0; i < n; i++) a[i] = value++;
}
int main(void)
{
size_t n = 1;
scanf( "%zu", &n );
int a[n];
iota( a, n, 1 );
for ( size_t i =0; i < n; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
return 0;
}
If to enter 10
then the output will look like
1 2 3 4 5 6 7 8 9 10
As for your program then in this loop
do
{
i = 0;
a = a - i;
array[a - 1] = a;
i++;
}
while (a > 0);
the variable a
is not being changed because in the beginning of each iteration the variable i
is set to 0.
You could write the loop like
i = 0;
do
{
array[a - i - 1] = a - i;
i++;
} while ( i != a );
Take into account that this statement
printf("Resulting array is %d", array[a]);
does not make sense. If you are going to output an array element by element you have to use a loop as for example it is shown in the demonstrative program above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.