multiples of 7

Hey every1, i am trying to write a program which fills in an array of integers which r multiples of 7 between -63 and 1000; but it doesnt seem like its working for me...heres the code:-

#include "stdafx.h"
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include<conio.h>
using namespace std;


int main()
{
float db7;
int i;
int a=7;
cout<<"Multiples of 7\t\t"<<endl;
for (i=-63;i<=1000;i++)
{ db7 = a * (i % a);
cout<<setprecision(2)<<"\n"<<db7;

}

return 0;
}
Put it in code tags! You can't just throw code at people! It's practically illegible!
(I'm sorry but I've said that so many times.)
Now what isn't working and what output are you trying to get?
By the way this is a civilized forum, and we don't abbreviate are or one. If you don't have the time to type out a, then r, then e, or o, then n, then e, I definitely don't have the time to answer.
Last edited on
My apologies, I am just in a hurry when I type. My teacher asked me to create a program which fills in an array of multiples of 7 between -63 and 1000; then he asked me to use the function

int binsearch(int x, int v[], int n)

to find out in which element the value 721 is stored, print this information to the screen. And output some informative message, in case you have mistyped the value, for example as 712.

I am very new to the C++ world and I am using Microsoft Visual Studio 2008 to write the program and all I have reached to till now is the following ( I do not understand what you mean to put my codes in tags, I just copy and paste, I am sorry if that is causing any inconvenience for you):-


int main()
{

int db7;
int i;
int a=7;
int set;
cout<<"Multiples of 7\t\t"<<endl;
for (i=-63;i<=1000;i++)
{ db7 = i % a;
if (db7 == 0)
{
int set[] = {i};
int binsearch (int x, int y)
{ if ( x = set[y])
{
cout<<" The element in which the value 721 is stored is "<<x;
}
}

}

}

return 0;
}
By code tags he mean a function on this forum to make code easier to read(i will show you an example). If you are new to c++ i really recommend you take a look at the tutorials on this site, or your own resource, and not try and attempt programs that are above your current skill level; it will only waste your time. Take a look at this program and try and follow it, it is based on your OP:

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
#include <iostream>

int main()
{
    int multsOfSeven[1024];
    int x = 0;
    
    for(int i=-63;i<=1000;i++) {
      if(!i)                                // can be writen as: if(i != 0) ... i just prefer my syntax
        continue;                            // skip to next iteration
      if(!(i%7))                            // again, if((i % 7) != 0) ...
          multsOfSeven[x++] = i;    // add the value of i to the array "multsOfSeven" at element i then increment it.
          /* you could write:
          multsOfSeven[x] = i;
          x++;
          */
    }
    
    for(int i=0;i<x;i++)
      std::cout << multsOfSeven[i] << "\n"; // out the contents of the array
    
    
    std::cin.get();
    return 0;
}
Alright, thanks alot for the help on that; I am not trying to create programs above my level, it is just that I have an a C++ assignment to finish and I have been working on it ever since yesterday; there were two parts that I was stuck on but the first part, I managed to finish it with the help of someone on this forum. Now this is the second part, you have helped me through the first portion of it but I do not know how to execute the second portion which the teacher asked me to do:-

use the function

int binsearch(int x, int v[], int n)

to find out in which element the value 721 is stored, print this information to the screen. And output some informative message, in case you have mistyped the value, for example as 712.
Hmmm... I can understand the first and second parameters but I have no idea why you would need the third? Also, I don't unserstand the whole mistyping the value part; if you program is running a for loop and working the values out for you, where is there any room for user input let alone user mistakes?
The same result(modify from mcleano's):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    int multsOfSeven[1024];
    int x = 0;
    
    for(int i=-63;i<=1000;i+=7) {
	if(!i)continue;
	multsOfSeven[x++]=i;	
    }
    
    for(int j=0;j<x;j++)
      std::cout << multsOfSeven[j] << "\n"; 
    
    
    std::cin.get();
    return 0;
}

Thanks gcampton.
Last edited on
guys when making posts, look at the all the little buttons on the right ----->
those buttons there are to format your posts, the <> button places code tags in you posts
[ code] tags [/code ] and the same goes for quotes, the quote button is a picture of inverted commas, bold is a big B italic is a big I underline is a big underlined U, etc etc etc


while a lot of simple examples don't particularly need code tags, it's still good practice to use them.
Last edited on
@bolo89... The only part of my program that you have modified(in fact, removed) is a very important line. I put it there for a reason. The modulus of 0 is undefined in most systems so you need to check the value of 'i' before you perform the operation.
@mcleano... Thanks, I modified my code to ckeck the value of 'i'. Thanks very much, mcleano.
Last edited on
Topic archived. No new replies allowed.