GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Oct-2006, 10:11
killer9012 killer9012 is offline
Member
 
Join Date: Mar 2004
Posts: 137
killer9012 is an unknown quantity at this point

Table


I am trying to creat a program that will allow me to intialise the values 2 through 1000 in a table, set all their values to 1, then from there run a program that will find, which value is a first number (only dividable by its self and 1) im assuming using the % with no rest would find these values, and when these numbers are found in the table, change their value to 0, continue to the next number, eliminate its mulptiples, continue untill only first numbers remain, and then the program should display these numbers in a table. How would i go about doing this. My first problem is my program wont even cout my table with values from 1-1000, it goes from 706 to 1001.....

CPP / C++ / C Code:
#include <iostream.h>
#include <stdlib.h>	

void main()
{
	const  Tableau=1000;
	int s[ Tableau ] = {1};
	int numero;

	for( numero = 0 ; numero <= 1000  ; numero++ )
	{
	
			s[numero]++;

			cout << numero << " " << s[numero] << endl;

	}
}
thats my program to date, numero=number, tableau=table (its for a french info class)

any ideas, helps tricks?
  #2  
Old 09-Oct-2006, 12:29
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Table


Not a clue what you are trying to do except

Quote:
Originally Posted by killer9012
...set all their values to 1...
If all you want is everything = to 1, set everything = 1.
CPP / C++ / C Code:
#include <iostream.h>    // very old header.  Use the one without the .h
#include <stdlib.h>	

void main()    // main() is an int, not a void
{
	const  Tableau=1000;
	int s[ Tableau ];         // simple since you are setting all values anyway
	int numero;

	for( numero = 0 ; numero <= 1000  ; numero++ )
	{
//		s[numero]++;
		s[numero] = 1;      // much easier to read and to the point

		cout << numero << " " << s[numero] << endl;

	}
}
About main(), see this
__________________

Age is unimportant -- except in cheese
  #3  
Old 09-Oct-2006, 12:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Table


Quote:
Originally Posted by killer9012
My first problem is my program wont even cout my table with values from 1-1000, it goes from 706 to 1001.....

CPP / C++ / C Code:

	const  Tableau=1000;
.
.
.
	int s[ Tableau ] = {1};
.
.
.
	

    for( numero = 0 ; numero <= 1000  ; numero++ )
    {
    s[numero]++;


You declare an array that has 1000 elements. Index values for the elements go from zero to 999, not zero to 1000.

There are several violations of the C++ standard that will not allow the program to compile in my compiler (GNU g++). I have made it comply with Standard C++ as follows:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main() // C++ standard requires the return type of main() to be int
{
    const int Tableau = 1000; // The declaration without the int is not allowed
    int s[Tableau] = {1}; //This sets s[0] to 1, and all others to 0
    int numero;

    for (numero = 0; numero <= 1000; numero++) { // the loop will go beyond the array limits
        s[numero]++;
        cout << numero << " " << s[numero] << endl;
    }
    return 0; // not absolutely necessary, but considered cosmetically preferable by many
}

Now, I didn't fix your bug: you must not use an index value of 1000 since the array only has elements from 0 through 999.

Also, if you want to initialize the array to have values of all 1, you must use a loop. There is no way (no way) that a declaration statement can initialize array elements to anything other than zero other than giving a list of values.

You can't use an assignment statement to give values to multiple array elements.

Regards,

Dave
  #4  
Old 09-Oct-2006, 13:20
killer9012 killer9012 is offline
Member
 
Join Date: Mar 2004
Posts: 137
killer9012 is an unknown quantity at this point

Re: Table


lets say i set em all to 0, then want to creat a program that will pick out first numbers, and change their value to 1, then at the end of the program, display the table with only values of still 1 to show, 0 being not first numbers, such as 4,6,8,9, etc. Basicly, i need the program to find every number thats a multitple of another, and elimante it.)? how would i do this?
  #5  
Old 09-Oct-2006, 13:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Table


Quote:
Originally Posted by killer9012
lets say i set em all to 0, then want to creat a program that will pick out first numbers, and change their value to 1, then at the end of the program, display the table with only values of still 1 to show, 0 being not first numbers, such as 4,6,8,9, etc. Basicly, i need the program to find every number thats a multitple of another, and elimante it.)? how would i do this?

In C or C++ you can use the modulus operator '%' to determine whether one integer is an exact multiple of another.


For integer data types x and y, with y not equal to zero, then the value of the expression x%y is equal to zero if and only if x is an exact multiple of y. That is the same as saying y is a factor of x if and only if the value of x%y is equal to zero.

CPP / C++ / C Code:
    if ((x % y) == 0) {
        /* do whatever you need to do when y is a factor */
    }
    else {
        /* do whatever you need to do when y is not a factor */
    }

Regards,

Dave
  #6  
Old 09-Oct-2006, 13:49
killer9012 killer9012 is offline
Member
 
Join Date: Mar 2004
Posts: 137
killer9012 is an unknown quantity at this point

Re: Table


CPP / C++ / C Code:
#include <iostream.h>

void main()
{
	int Tableau[1000] = {0};
	int numero;
	int k;

	for( numero = 2 ; numero <= 1000  ; numero++ )
	{
		for (k=1 ; k <= numero ; k++ )
		
		if (numero % k == 0)
		{
		Tableau[numero]++;
		Tableau[numero] = 1;
		}
		else
		{
			Tableau[numero] = 0;
		}
			cout << numero << " " << Tableau[numero] << endl;

	}

	cout << endl << endl << "Fin de l'execution du programme." << endl << endl ;
}



what i got just to date, gives me the values from 706 to 1000, giving every value in my table 0, and at the end of execution, i get a microsoft error, program must be shut down, blah blah.
I use iostream.h and void main, cause if not, ms visual c++ bitches at me.
  #7  
Old 09-Oct-2006, 14:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Table


Quote:
Originally Posted by killer9012
[and at the end of execution, i get a microsoft error, program must be shut down, blah blah.

I hate to repeat myself, but when you declare an array like int Tableau[1000] it is only legal to use index values from 0 to 999, not 0 to 1000.

Change your main() to:

CPP / C++ / C Code:
{
    int Tableau[1000] = { 0 }; // unnecessary since I assign values later, but it doesn't hurt anything
    int numero;

    for (numero = 0; numero < 1000; numero++) {
        Tableau[numero] = 1;
    }

    for (numero = 0; numero < 1000; numero++) {
        cout << "Tableau[" << numero << "] = " << Tableau[numero] << endl;
    }

    cout << endl << endl << "Fin de l'execution du programme." << endl <<
        endl;
}

Now, it will print values of Tableau[0] up to and including Tableau[999]. Note that if are using a command window with default properties, it probably only has a buffer length 0f 300, which means that Tableau[0] through Tableau[705] have scrolled off of the screen. If you want to see all of the values, then you can either


1. Redirect standard output to a file and look at the file in a text editor.

or

2. Increase the screen buffer (layout in the "Properties" dialog box) to something more than 1000


Regards,

Dave

P.S. If you use the following as your main file, all standard C++ compilers will be happy:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main()
{
    int Tableau[1000] = { 0 };
    int numero;
 
    for (numero = 0; numero < 1000; numero++) {
        Tableau[numero] = 1;
    }

    for (numero = 0; numero < 1000; numero++) {
        cout << "Tableau[" << numero << "] = " << Tableau[numero] << endl;
    }

    cout << endl << endl << "Fin de l'execution du programme." 
         << endl << endl;

    return 0;
}

Maybe you don't care about other compilers or other people's opinions, but if you ever get out from under Microsoft Visual C++, you may find that certain programming habits will not serve you well. I'm just trying to help. The program that I show above works just fine with Microsoft Visual C++ version 6.0 as well as other C++ compilers to which I have access (Borland, GNU, Microsoft Visual C++ 2005, GNU).

Free advice, freely given (and just my opinion): Take it or leave it.

D.
  #8  
Old 09-Oct-2006, 14:44
killer9012 killer9012 is offline
Member
 
Join Date: Mar 2004
Posts: 137
killer9012 is an unknown quantity at this point

Re: Table


ok, so now that i have that problem fixed, how can i get the program to find first numbers, and change the value of the said number in my table to 1, since they all have a starting value of 0. And dave i do appretiate ur help, im just a noob trying to learn, so please resist the urge to punch me
  #9  
Old 09-Oct-2006, 16:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Table


Quote:
Originally Posted by killer9012
im just a noob trying to learn

I didn't mean to sound harsh, but I guess it did, and I apologize. I mean well, but sometimes it just comes out wrong. I really want to see you have success and to feel that you can ask anything, any time.

No one was born knowing this stuff (knowing programming languages or knowing how to help people either), and I am still learning. People usually have to tell me things three times before it soaks in, so if I only told you the same thing twice, you are one up on me. I'll try to be more patient, as I ask others to be patient with me.

Regards,

Dave
  #10  
Old 09-Oct-2006, 16:54
killer9012 killer9012 is offline
Member
 
Join Date: Mar 2004
Posts: 137
killer9012 is an unknown quantity at this point

Re: Table


haha, no worries man, where all noobs at some point! still not having any sucess with my math problem tho, it keeps setting all the numbers in my table to 1, so i guess the condition is always met,thus a problem in my code :S
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot create a table in DB WaltP MySQL / PHP Forum 3 26-Apr-2006 16:09
making table of LCM using arrays azncrazycooler C++ Forum 5 01-Dec-2005 03:52
Sorting a table to give each user a rank Wowl MySQL / PHP Forum 1 23-Apr-2005 14:45
C league table Networkedd C Programming Language 3 13-Mar-2005 17:20
[Tutorial] MySQL Basics nniehoff MySQL / PHP Forum 15 23-Mar-2003 19:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.