Thread: Teach Me: A program that finds multiples of 7 between a range of numbers

1. Teach Me: A program that finds multiples of 7 between a range of numbers

Similar to my previous thread this is an assignment for my intro to programming course. I am tasked with designing and writing a program that finds the multiples of 7 within a range of numbers and places them into an array. This range is inputted by the user and the program needs to keep a counter for the multiples of 7.

This is what I've written thus far.

Code:
```#include <stdio.h>
int main ()
{
char fname[] = "John";
char lname[] = "Doe";
int list[1429];
int iv1;
int iv2;
int count;
int n;
int m;

printf("Enter two integer values. It is required that the first value be smaller than the second. \n");
scanf("%i%i",&iv1,&iv2);
count = 0;

if (iv1 > iv2)
{
printf ("\nThe first value was greater than the second. Try Again\n");
}

for (n = iv1; n <= iv2; n++)
{
if (n % 7 == 0)
{
list[count] = n;
count++;
}
}

printf("\n%s %s\n",fname,lname);
printf("CSCI 1101 \n");
printf("C Project 5 \n\n");
if (iv1 < iv2)
{
for (m=0; m < count; m++)
{
printf("%d \n",list[m]);
}
printf("\n");
}
printf("Count %i",count);
}```
Things I've run into:

Is it possible to make the table a variable size that can increase with the amount of multiples of 7? Right now it's 1429 which is big enough for all the multiples of 7 between 0 and 9999.

This program registers 0 as a multiple of 7 and I don't know why.

Other than those two things I think this is good.

2. Is it possible to make the table a variable size that can increase with the amount of multiples of 7? Right now it's 1429 which is big enough for all the multiples of 7 between 0 and 9999.
Yes it is. One option is to use variable length arrays, a C99 feature. The safer option is to call malloc() (because it's been standard since basically forever) and allocate enough ints to fit the inputted range.
This page may help with calling malloc() - 11.1 Allocating Memory with <TT>malloc</TT>
This program registers 0 as a multiple of 7 and I don't know why.
Zero is a multiple of every integer and then some. If you want to exclude it, you have to edit your if to do so.

3. Other than those two things I think this is good.
if you look at your code flow, what is going to take place (line 17) if your if statement equals true, and what should it (maybe) do instead?

Code:
```    printf("Enter two integer values. It is required that the first value be smaller than the second. \n");
scanf("%i%i",&iv1,&iv2);
count = 0;

int retries = 1;
while ( iv1 > iv2 )
{
printf("\nThe first value was greater than the second. Try Again\n"
"Enter two integer values. It is required!!!!!!!!!\n"
"that the first value be smaller than the second. \n"
"Like I said the first time.\n"
"You, %s %s have made this same mistake %d time(s) now. BTW\n"
"Care to make it another one, hummmm?\n",
fname, lname, retries);
scanf("%i%i",&iv1,&iv2);
retries++;
}```
just a little friendly programming design hint.