Finding multiples of 7 in given number range

I have this assignment to write a program the asks the user to enter two numbers between 0 and 999 and then it will find all the multiples of seven in between the two numbers. We were given the basic outline and had to fill in certain parts, here's what I have so far, the question marks are what I need have left, I've tried a couple different variations but i'm obviously not doing it right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MSIZE 500


using namespace std;
int fmul7(int str, int endr, int mul7[],int m7size);
void outresults(int sr, int er, int m7[], int szm7, int cm7);
int main(int argc, char *argv[])
{
    int mul7[MSIZE];
    int n;
    int sr;
    int er;
    int c7;
    
    printf("Enter start and end range values: ");
    while((n=scanf(" %i %i", &sr, &er))==2)
       {
       if(sr<0||er>999||sr>er)
       printf(" Range value out of range\n");
       else
       {
           c7=fmul7(sr,er,mul7,MSIZE);
           outresults(sr,er,mul7,MSIZE,c7);
           printf("Enter start and end range values:");
           }
           }
       if(n!=EOF)
          printf("Input error occured in scanf \n");
       system("PAUSE");
       return 0;
       }
       
       /***********************************************/
int fmul7(int str, int endr, int mul7[],int m7size)
{
    int ndx;
    int rem;
    int i;
    
    i=0;
    for(ndx=???;ndx<=????;ndx=ndx+1)
    {
        rem=ndx%7;
        if(rem==?)
        {
           mul7[?]=???;
           i=i+?;;
           }
           }
           return ?;
           }
    /*********************************************************/
void outresults(int sr, int er, int m7[], int szm7, int cm7)
{
     int i;
     
     printf("Start Range= %i   End Range= %i \n",sr,er);
     printf("Multiple of 7 count is: %i \n", cm7);
     printf("Multiple of 7 valuers are: \n");
     for(i=0; i<cm7;i++)
              printf(" %i \n", m7[i]);
              }
              
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
What did the basic outline look like? What you have is extremely over complicated for what you have to do.
This is the basic outline he gave us lol I got it to this now, but Its giving me an error saying it needs a declaration before '}' on line 67 then it also says i have a building error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MSIZE 500


using namespace std;
int fmul7(int str, int endr, int mul7[],int m7size);
void outresults(int sr, int er, int m7[], int szm7, int cm7);
int main(int argc, char *argv[])
{
    int mul7[MSIZE];
    int n;
    int sr;
    int er;
    int c7;
    
    printf("Enter start and end range values: ");
    while((n=scanf(" %i %i", &sr, &er))==2)
       {
       if(sr<0||er>999||sr>er)
       printf(" Range value out of range\n");
       else
       {
           c7=fmul7(sr,er,mul7,MSIZE);
           outresults(sr,er,mul7,MSIZE,c7);
           printf("Enter start and end range values:");
           }
           }
       if(n!=EOF)
          printf("Input error occured in scanf \n");
       system("PAUSE");
       return 0;
       }
       
       /***********************************************/
int fmul7(int str, int endr, int mul7[],int m7size)
{
    int ndx;
    int rem;
    int i;
    
    i=0;
    for(ndx=str;ndx<=endr;ndx=ndx+1)
    {
        rem=ndx%7;
        if(rem==0)
        {
           mul7[i]=m7size;
           i=i++;;
           }
           }
           return ndx;
           }
    /*********************************************************/
void outresults(int sr, int er, int m7[], int szm7, int cm7)
{
     int i;
     
     printf("Start Range= %i   End Range= %i \n",sr,er);
     printf("Multiple of 7 count is: %i \n", cm7);
     printf("Multiple of 7 valuers are: \n");
     for(i=0; i<cm7;i++)
              printf(" %i \n", m7[i]);
              } 
              }
              

    system("PAUSE");
    return EXIT_SUCCESS;
}
It looks like you have a couple extra closing brackets there.
1
2
3
4
5
6
7
8
9
10
11
12
13
     int i;
     
     printf("Start Range= %i   End Range= %i \n",sr,er);
     printf("Multiple of 7 count is: %i \n", cm7);
     printf("Multiple of 7 valuers are: \n");
     for(i=0; i<cm7;i++)
              printf(" %i \n", m7[i]);
              }  // no mate 
              }  // no mate
              

    system("PAUSE");
    return EXIT_SUCCESS;

I'm not sure what to make of it if this is basic? The array is pointless except to pass it to a function that serves no purpose other than to use the array. Even if you needed a print a function, it still only needs 1/2 the code and 5 variables at most. Less if you just do it all in main().
If you think about this mathematically, you will see that this is actually easier than you are making it to be.

If you are given the range x -> y, all you need to do is find the first multiple of seven in that range.

So the first part of your program just needs to iterate that range and find the first number in that range that is divisible by seven

Then the next part is to store those values in an array, and this is even easier. Simply keep adding seven to that number you found the first time and keep storing these in an array until the value you have is larger than the end of the range

Ex. 3 -> 30
Find first multiple of seven
3, 4, 5, 6, 7 <-- found it

Next start adding seven
7, 14, 21, 28, 35 <-- larger than end of range, so don't store
I would replace that "find first" iteration with modulus. Depends on what the student should learn at this point.

As keskiverto said, I would use modulus.

1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>

int main()
{
	int nNum[10] = { 1, 4, 7, 14, 19, 12, 15, 18, 21, 24 };

	for (int i = 0; i < 10;i++)
		if (nNum[i] % 7 == 0)
			std::cout << nNum[i] << std::endl;
}
7
14
21
The "original template" had something like that already:
1
2
3
4
5
    for(ndx=str;ndx<=endr;ndx=ndx+1)
    {
        rem=ndx%7;
        if(rem==0)
        {

I was referring to Smac89's "Find first multiple of seven"
1
2
const auto rem { str % 7 };
auto first { str + (rem) ? (7-rem) : 0 };

Oh yes, missed that :)
Well I guess its technically only half of the outline, the other half is to in addition to finding the multiples of seven, find all numbers containing the number seven and their averages, here's what he gave us originally and told us to fill in the question marks does it make more sense with the rest or is it still more complicated then necessary? lol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MSIZE 500
#define DSIZE 500

using namespace std;

int main(int argc, char *argv[])
{
    int mul7[MSIZE];
    int dig7[DISZE];
    int n;
    int sr;
    int er;
    int c7;
    int d7;
    double mavg;
    double davg;
    
    printf("Enter start and end range values: ");
    while((n=scanf(" %i %i", &sr, &er))==2)
    {
                     if(sr<0||er>999||sr>er)
                     printf("range value out of range\n");
                     else
                     {
                         c7=fmul7(sr,er,mul7,MSIZE);
                         d7=fdig7(sr,er,dig7,DSIZE);
                         mavg=calcavg(mul7,c7);
                         davg=calcavg(dig7,d7);
                         outresults(sr,er,mul7,MISZE,dig7,DSIZE,c7,d7,mavg,davg);
                         printf("Enter start and end range values:");
                         }
                         }
                     if(n!=EOF)
                     printf("Input error occurred in scanf\n");
                     system("PAUSE");
                     RETURN 0;
                     }
    system("PAUSE");
    return EXIT_SUCCESS;
}
}
/************************************************/
int fmul7(int str, int endr, int mul7[], int m7size)
{
    int ndx;
    int rem;
    int i;
    
    i=0;
    for(ndx=???;ndx<=????;ndx=ndx+1)
    {
          rem=ndx%7;
          if(rem==?)
          {
            mul7[?]=???;
            i=i+?;
            }
            }
            
            return ?;
            }
    /****************************************************/
int fdig7(int str, int endr, int dig7[], int d7size)
{
    int ??;
    int ???;
    int ??;
    int ??;
    int ??;
    int ????;
    
    c7=0;
    
    for(???=str; ???<=endr; ???++)
    {
       q1=???/100;
       rem1=num-(q1*100);
       q2=rem1/10;
       q3=rem1-(q2*10);
       if(q1==?||q2==?||q3==?)
       {
          dig7[??]=???;
          c7++;
          }
          }
          return ??;
          }
/**************************************************/
double calcavg(int md7[], int cntmd7)
{
       int ndx;
       int sum;
       double avg;
       
       sum=0;
       avg=0.0;
       
       for(ndx=?; ndx<cntmd7;???+++)
       sum=sum+md7[???];
       if(cntmd7==?)
            printf("Average divisor is zero - skip division \n");
       else
             avg=(double)sum/??????;
             return ???;
             }
/**********************************************************/
void outresults(int sr, int er, int m7[], int sxm7, int d7[], int szd7, int cm7, int cd7, couble mav, double dav)
{
     int i;
     
     printf("Start Range= %i    End Range= %i \n", sr, er);
     printf"Multiple of 7 count is:  %i \n", cm7);
     printf("Number having digit 7 count is:  %i \n", cd7);
     printf("Multiple of 7 values are: \n");
     for(i=0; i<cm7;i++)
              printf(" %i \n", m7[i]);
     printf("Values having digit 7 are: \n");
     for(i=0;i<cd7;i++)
              printf(" %i \n", d7[i]);
     printf("Multiple of 7 average is:  %0.21f\n", mav);
     printf("Values having digit 7 average is:  %0.21f\n", dav)
     }    
Last edited on
it's easy, it the user input 2 number;

cin>> x >> y;

then use loop , for or while for finding first multiple of 7 between x and y, if first integers is found, then break; to halt the loop.

1
2
3
4
for(x=? ; x<=y ; x++)
{if(x%7==0)
cout << ??? ; 
break;}


or

1
2
3
4
5
while(x<=y){
if(x%7==0)
{cout << ???;
break;}
x++;}


Tips:remove break; for cout every numbers that is multiply of 7
What are you supposed to learn from this exercise?
10 arguments to print 7 results?
void outresults(int sr, int er, int m7[], int sxm7, int d7[], int szd7, int cm7, int cd7, couble mav, double dav
Besides double being spelled wrong the first time, it is very easy mix up which one does what and get them in the wrong order.

150 is more than enough.
int mul7[MSIZE]; //MSIZE = 500
Since you do not know the size and if will vary anyway depending on user input, a static array is about the worst container to use for this. You cannot directly pass them to functions either.

I could easily write this from scratch, but have no idea how to go about just filling in the missing pieces without rewriting most of what is given. I would start by making all the functions void and only use two arguments for each of them if I was handed this and told to fix it.



This all just seems a crazy way to teach a student how to program in c++. errm here's some source code, fill in the blanks where I have ???.

The amount of function parameters is overkill for what you need.
Topic archived. No new replies allowed.