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

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    7

    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. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    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. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    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.
    Last edited by userxbw; 11-27-2017 at 08:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-30-2012, 02:51 PM
  2. Replies: 10
    Last Post: 09-22-2010, 02:03 AM
  3. program that finds antiderivative
    By john5754 in forum C++ Programming
    Replies: 18
    Last Post: 12-12-2008, 04:22 PM
  4. Program that finds the middle number
    By Zerohero11 in forum C++ Programming
    Replies: 17
    Last Post: 10-13-2005, 07:20 PM
  5. counting common multiples of 2 numbers
    By j0nnyX in forum C Programming
    Replies: 1
    Last Post: 10-26-2004, 06:20 PM

Tags for this Thread