0

I'm working on the Credit problem set. Along the way, I've encountered a scenario where I want to use a for loop to store multiple values. If I type printf("%d", cc_number[p]); in that for loop, it will start at array position number 14 and decrements by two until it hits array position number 0, and it will print out those numbers.

What I want to know is how do I go about storing those values inside of either one variable containing the 8 different numbers or 8 different variables containing 1 number each.

Thank you.

#include <stdio.h>
#include <stdlib.h>

int main()
{
long long int cc_number[16] = {4,0,0,0,0,5,7,8,0,0,9,8,2,0,5,2};
int p;


for (p = 14; p >= 0; p = p -2){

}
return 0;
}
1

Since you have identified in your code sample above that you understand how to declare and put values in an array. Then I suggest the simple means is to just create an array of 8 length that can store the values. Also in a for loop, in the initialization part you can declare more than one variable. So from your code sample you could do something like:

int main()
{
    char cc_number[16] = {4,0,0,0,0,5,7,8,0,0,9,8,2,0,5,2};
    // Initialize all indexes of evens to 0
    char evens[8] = {0};

    for (int at = 14, i = 0; at >= 0 && i < 8; at -= 2, i++){
        evens[i] = cc_number[at];
        printf("%d\n", evens[i]);
    }
    return 0;
}

Good luck going forward.

| improve this answer | |
  • If I'm going through your code correctly, I came up with something similar on my own. – Trifonas-Kaoulla Jan 3 at 2:25
  • Good. Keep it up! Usually problems can be solved by taking a break and just thinking for a bit. Plus a little reading. Glad to see you solved your problem. – Nicknapoli82 Jan 3 at 2:28
  • Sorry about that initial comment. I hit enter to create a new paragraph, but it submitted that comment instead. After looking at your code and the one I wrote, i realize now that mine is not the same as yours. Rather than writing one for loop with multiple initializations and conditions and increments/decrements, I wrote two for loops--a nested for loop-which, I now see, are compiled differently than a for loop like yours. You just taught me that that is even possible haha. Yours makes more sense because the two variables change together rather than one changing first before the other – Trifonas-Kaoulla Jan 3 at 2:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .