At the outset, I want to say that I tried to complete this task with arrays and don't know how to make it witout them.
Here is my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
long n = get_long("Card number: ");
long nc = n; // to define how many digits in a card number; after first loop gets to 0
int x = 0;
int c = 0; // to store the answer after first step of the algorithm. *MAIN NUMBER*
int c1 = 0; // rest. *MAIN NUMBER*
int a; // *MAIN NUMBER*
//defines how many digits in a card number
for (int y = 0; nc > 0; y++)
{
nc /= 10;
x = y + 1;
}
// stores every number in the array
int nm [x]; // main array
int v = 0; // how many in each part
int half1 [0]; // to be multiplied by 2
int half2 [0]; // rest
for (int z = 0; z < x; z++)
{
nm [z] = n % 10;
n /= 10; // я не понимал, почему значение *n* не обновляется. оказалось, что я просто забыл *пересобрать эту переменную*
}
if (x % 2 == 0)
{
half1 [x / 2];
half2 [x / 2];
half1 [0] = nm [0];
}
else
{
half1 [x / 2 + 1];
half2 [x / 2];
half1 [0] = nm [0];
}
for (int b = 2, g = 1; b < x; b += 2, g += 1, v++) // when solve this problem try to change variable name b to z
{
half1 [v] = nm [b];
half2 [v] = nm [g];
}
for (int h = 0; h < v; h++)
{
half1 [h] *= 2;
c1 += half2 [h];
}
for (int h = 0; h < v; h++)
{
if (half1 [h] >= 10)
{
half1 [v + 1] = half1 [h] / 10;
half1 [v + 2] = half1 [h] % 10;
}
else
{
c += half1 [h];
}
}
if(a % 10 != 0)
{
printf("INVALID CARD NUMBER\n");
}
else if ((nm [0] == 3 && nm [1] == 4) || (nm [0] == 3 && nm [1] == 7))
{
printf("AMEX\n");
}
else if ((nm [0] == 5 && nm [1] == 1) || (nm [0] == 5 && nm [1] == 2) || (nm [0] == 5 && nm [1] == 3) || (nm [0] == 5 && nm [1] == 4) || (nm [0] == 5 && nm [1] == 5))
{
printf("MASTERCARD");
}
else if (nm [0] == 4)
{
printf("VISA\n");
}
}
Now I got problelms there, from line 33 to 45:
if (x % 2 == 0)
{
half1 [x / 2];
half2 [x / 2];
half1 [0] = nm [0];
}
else
{
half1 [x / 2 + 1];
half2 [x / 2];
half1 [0] = nm [0];
}
And have got such an output at the terminal. I thought this part of my code would define size of an array, because it isn't strict and depends on how many is a card number.
half1[x/2]
doesn't do anything, what are you trying to do there? same iwth the next line. not sure why everytime you go through the loop you want to set half1[0] equal to nm[0]. in fact you are doing that either way.. i dont think you understand how to manipulate arrays. nothing is happeninng at all in this code... except that either way you are assinging the 0th element of half1 to the value of the 0th element of nm.