I'm still a little confused. Basically this is what I'm trying to do, and I'm having one hell of a time with it...

My program has two values "Value" and "N". The goal of the program is to select every Nth number of Value starting with the last, and print it out. For example...

VALUE--N------ RESULT
12345, 1 select 1 2 3 4 5
12345, 2 select 1 2 3 4 5
12345, 3 select 1 2 3 4 5
12345, 4 select 1 2 3 4 5
12345, 5 select 1 2 3 4 5

So far I got my code to store every number of Value into separate elements of an array. But I'm having the hardest time getting the Nth number of value to print out in a concatenation.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <math.h>

uint64_t everyNth(uint64_t Value, uint8_t N)
{
    int numDigits(int num)
    {
        int length = 0;
        while(num >= 10)
        {
            num = num/10;
            length++;
        }
        length++;
        return length;
    }

    int numLength = numDigits(Value);
    int array[numLength];

    int i;
    int lastDigit;
    int minusLast;
    for(i=0;i<numLength;i++)
    {
        lastDigit = Value % 10;
        array[i] = lastDigit;
        minusLast = Value - lastDigit;
        Value = minusLast / 10;
    }

    int first = array[0];

    return secArray[0];
}

int main()
{
    uint64_t Value = 12345;
    uint8_t  N     = 1;

    uint64_t Result = everyNth(Value,N);

    printf("%21"PRIu64"%3"PRIu8"%21"PRIu64"\n", Value, N, Result);

    return 0;
}

Hmm, are you allowed to nest a function within a function? Like you have in your everyNth() function with the nested numDigits() function?

Another thing is you cannot declare a static array with a variable. On line 23 you declared int array[numLength] which is not allowed. You must use a constant number or a pointer. To create an array of any size you want you have to use a pointer.

int num_elements;
int* my_numbers;

// grab the number of elements you want
scanf("%d", &num_elements);

// ask the system to reserve n bytes for my array of integers
// each integer is 4 bytes
numbers = malloc(num_elements * sizeof(int));

Your code seems right, all you have to do is return array[N-1] for the Nth element.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.