Good evening, I'm doing an exercise in C. The result is correct, but I get the following error in Visual Studio:. "Run-Time Check Failure # 2 - Stack around the variable 'n' was corrupted"
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#define TAM 1000
void main()
{
int i, a, t, n[TAM];
i = 0;
printf("Enter a value T: ");
scanf("%i", &t);
if (t >= 2 && t <= 50)
{
while (i < TAM)
{
for (a = 0; a < t; a++)
{
n[i] = a;
i++;
}
}
for (i = 0; i < TAM; i++)
{
printf("%i\n", n[i]);
}
}
system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#define TAM 1000
void main()
{
int i, a, t, n[TAM];
i = 0;
printf("Enter a value T: ");
scanf("%i", &t);
if (t >= 2 && t <= 50)
{
while (i < TAM)
{
for (a = 0; a < t; a++)
{
n[i] = a; //!!! you're not checking if subsequent i values are less than TAM
i++; //HERE, you increment i without checking if(i < TAM)
}
}
for (i = 0; i < TAM; i++)
{
printf("%i\n", n[i]);
}
}
system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#define TAM 10
void main()
{
int i, a, t, n[TAM];
i = 0;
printf("Enter a value T: ");
scanf("%i", &t);
if (t >= 2 && t <= 50)
{
while (i < TAM)
{
for (a = 0; a < t; a++)
{
n[i] = a;
if (i < TAM)
{
i++;
}
}
}
for (i = 0; i < TAM; i++)
{
printf("%i\n", n[i]);
}
}
system("pause");
}