Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C plus plus


yours31f's Avatar
Retired
10 0

I am Practicing c plus plus and have run out of ideas. if you have something that you would like me to code post it here and ill post my script. Please note: if you want an exe please say that in your post.

anyway i will post anything i get so please NO FLAMES. I am really going to make these and will ignore stupid post as all of you should.


spyware's Avatar
Banned
0 0

Code a program that calculates two primes, P and Q and display every number in between them (P and Q are random).


ynori7's Avatar
Future Emperor of Earth
0 0

This isnt something that i want you to make for me, because i've already made it in C, but here's an idea that would be good for you unless you dont know as much about programming as you claim:

Write a program called baseConverter that takes in 3 arguements: number1, base1, base2

the program should then take number1 (which is a number of base=base1) and convert it into base=base2.

Example: baseConverter 1011.01 2 10 –this should output 11.25

the program should be able to handle bases from 2 to 36 and should be able to handle both integer and fixed point decimal numbers

you should be able to do this, it's not too tough once you come up with your conversion algorithms. wow, that really sounded like a teacher giving out a homework assignment.


yours31f's Avatar
Retired
10 0

#include <stdio.h> #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main () { /* initialize random seed: */ srand ( time(NULL) ); int p, q;

p = rand() % 10000000 +1;

q = rand() % 10000000 + p;

for (p; p<q;p+2){ cout<<p<<" "; } system("pause"); return 0; }


yours31f's Avatar
Retired
10 0

@ ynori7 i will get cracking in that. thx. sounds cool and challenging.


spyware's Avatar
Banned
0 0

P and Q need to be random PRIMES. Use the IsPrime command to check whether a number is a prime, this way you can loop a list of numbers through it.

It isn't exactly "calculating" primes but heck, it works.


ynori7's Avatar
Future Emperor of Earth
0 0

yours31f wrote: p = rand() % 10000000 +1;

q = rand() % 10000000 + p;

for (p; p<q;p++){ cout<<i1<<" "; }

you are outputting a variable called 'i1' which is never declared, and it never changes. also, your for loop should be increasing by 2 each time, not one because the program was supposed to display only the even numbers between p and q.

i'm curious, did this actually work when you tested it? or did you just post it without testing?


ghost's Avatar
0 0

Why don't you try do the HBH timed challenges?

Do could also try build a sudoku cracker.

Another idea would be to make an attractive screen saver. You know, something like window's old maze screen saver. Of course, in your case, you would want to make something more simpler.


yours31f's Avatar
Retired
10 0

i don't dont't like you yroni7, i have still not figured out how to get it to change i know there is a command but i was trying to program it myself.


ghost's Avatar
0 0

yours31f wrote: i don't dont't like you yroni7, i have still not figured out how to get it to change i know there is a command but i was trying to program it myself.

if you know how to convert it then it is easy :), don't use any library, just use some math


ynori7's Avatar
Future Emperor of Earth
0 0

yours31f wrote: i don't dont't like you yroni7, i have still not figured out how to get it to change i know there is a command but i was trying to program it myself.

you dont like me or you dont like my challenge?

i didnt use a command to convert the bases, there isnt one that works universally, just for converting between decimal, hex, and binary i think. try googling for some algorithms, there's lots of them out there.

by the way, you spelled my name wrong.


yours31f's Avatar
Retired
10 0

yes your challenge and i noticed that i spelled it wrong (SRRY) but i will try to google them cuz so far all i figured out are these two things.

  1. for 1 or 0 (first digit (binary))

    if number % 2 = 1; cout >> "1" else cout >> "0"

  2. go and set all of them and test them but it will take way to long.


ghost's Avatar
0 0

well, I will give you a little hint in converting from binary to decimal and back, ok, so we have number … eg…. 1101, it is: 1x8 + 1x4 + 0x2 + 1x1 = 13… why this? Well, look, that thing is 1,2,4,8,16,32,64, etc…

back, ok, we have number 13, what is the biggest from 1,2,4,8,16,32, etc? well, 8, how many times? Just once, logical :-), so you have 1 in binary and 5 in decimal left…, ok, so what next? 4? Yes, you can, so 1 in binary again and 1 is left in decimal… can you use 2? No, there is just 1, so 0 in decimal… last one is 1… so result is 1101.

The same you can use for hex, octals, etc.


yours31f's Avatar
Retired
10 0

well that goes back to part 2 of my last post. im still looking for some mathematical approach to it.


ynori7's Avatar
Future Emperor of Earth
0 0

i'm in a good mood, so i'll give you one algorithm, but you're on your own for the rest: http://www.hitxp.com/math/arth/131202.htm

edit: by the way, that was on the first page of the first google search i just did, so it's not hard information to find.


ghost's Avatar
0 0

dude, try to show a short example of recursive programming…..

or how bout sum Bubble sort or Quick sort or Merge sort……..

B)


yours31f's Avatar
Retired
10 0

finally one that works well.

#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;

using namespace std;
int DecToBase(int,long,char*);

int BaseToDec(char*,int);

int GetIndex(char *,char);

//Symbols used to display a number correctly
//Numbers over base 10 use letters to represent values over and equal to 10
//You should be able to increase the max no. of bases by adding other symbols
//Remember a string needs 1 extra space for null character
char symbols[37] = &quot;0123456789ABCDEFGHIJKLMNoPQRSTUVWXYZ&quot;;
const int MAX_BASE = 36; //Highest base allowed (make sure there are enough symbols first!)
//

int main(int argc, char* argv[])
{
	char number[256] = &quot;&quot;;
	cout &lt;&lt; &quot;Base conversion functions. Written by Yours31f&#92;n&#92;n&quot;;
	//DecToBase samples
	cout &lt;&lt; &quot;Examples...&#92;n&#92;n&quot;;
	DecToBase(2,10,number);
	cout &lt;&lt; &quot;10dec to binary (base 2): &quot; &lt;&lt; number &lt;&lt; endl;
	DecToBase(8,25,number);
	cout &lt;&lt; &quot;25dec to octal (base 8): &quot; &lt;&lt; number &lt;&lt; endl;
	DecToBase(3,49,number);	
	cout &lt;&lt; &quot;240dec to base 36 (hexatridecimal?): &quot; &lt;&lt; number &lt;&lt; endl;
	DecToBase(16,28,number);
	cout &lt;&lt; &quot;28dec to hexadecimal (base 16): &quot; &lt;&lt; number &lt;&lt; endl ;
	//I don&#39;t know why I wasted time working this one out... but 998453 spells LEET in base 36
	DecToBase(36,998453,number);
	cout &lt;&lt; &quot;998453dec to base 36: &quot; &lt;&lt; number &lt;&lt; endl &lt;&lt; endl;
	//BaseToDec samples
	cout &lt;&lt; &quot;4C9hex to decimal: &quot; &lt;&lt; BaseToDec(&quot;4C9&quot;,16) &lt;&lt; endl;
	cout &lt;&lt; &quot;1760octal to decimal: &quot; &lt;&lt; BaseToDec(&quot;1760&quot;,8) &lt;&lt; endl;
	cout &lt;&lt; &quot;1101011101bin to decimal: &quot; &lt;&lt; BaseToDec(&quot;1101011101&quot;,2) &lt;&lt; endl &lt;&lt; endl;
	//User input sample
	int iInput = 0,iBase = 2, iSrcBase = 10;
	cout &lt;&lt; &quot;Try another conversion...&#92;nEnter a number in any base between 2 and 36 (must be integer): &quot;;
	cin &gt;&gt; number;
	cout &lt;&lt; &quot;Enter a base to convert from: &quot;;
	cin &gt;&gt; iSrcBase;
	cout &lt;&lt; &quot;Enter a base between 2 and 36 to convert to: &quot;;
	cin &gt;&gt; iBase;
	//If the source base is not 10, convert it to 10 first
	if(iSrcBase!=10)
		iInput = BaseToDec(number,iSrcBase);
	else
		iInput = atoi(number);

	//If source base is not 10, and the destination base is, it is not nessecery to run DecToBase as the number
	//Has already been found in base 10
	if(iSrcBase!=10&&iBase==10)
	{
		cout &lt;&lt; number &lt;&lt; &quot; in base &quot; &lt;&lt; iSrcBase &lt;&lt; &quot; = &quot; &lt;&lt; iInput &lt;&lt; &quot; in base &quot; &lt;&lt; iBase &lt;&lt; endl &lt;&lt; endl;
	}
	else
	{
		if(DecToBase(iBase,iInput,number))
			cout &lt;&lt; iInput &lt;&lt; &quot; in base &quot; &lt;&lt; iSrcBase &lt;&lt; &quot; = &quot; &lt;&lt; number &lt;&lt; &quot; in base &quot; &lt;&lt; iBase &lt;&lt; endl &lt;&lt; endl;
		else
			cout &lt;&lt; &quot;Error: Base was out of bounds... (must be between 2 and 36)&#92;n&#92;n&quot;;
	}
	return 0;
}

int DecToBase(int base, long iDec, char* szString)
{
	//Check base is between 2 and 36
	if(base&lt;2||base&gt;MAX_BASE)
		return 0; //Failed
	//If input is 0, output is 0
	if(iDec==0){
		strcpy(szString,&quot;0&quot;);
		return 1;
	}
	
	int count = 0;
	char chResult[256] = &quot;&quot;;
	char* pChResult = chResult;
	while(iDec &gt; 0 && count++&lt;256)
	{
		*pChResult = symbols[iDec % base];
		pChResult++;
		iDec = iDec/base;	//iDec = itself divided by base
	}
	strcpy(chResult,_strrev(chResult));	
	strcpy(szString,chResult);

	return 1;
}

int BaseToDec(char* number, int base)
{
	if(base&lt;2||base&gt;MAX_BASE)
		return 0; //Failed

	int NumLength = strlen(number);
	int PlaceValue, total = 0;
	//Work out the place value of the first digit (base^length-1)
	PlaceValue =  int pow(base,NumLength-1);
	
	//For each digit, multiply by its place value and add to total
	for(int i=0;i&lt;NumLength;i++)
	{
		total += GetIndex(symbols,*number)*PlaceValue;
		number++;
		PlaceValue /= base; //Next digit&#39;s place value (previous/base)
	}
	return total;
}


int GetIndex(char * pString, char search)
{
	int index = 0;
	while(*pString != (char)0) //Loop will finish at null character if no match is found
	{
		if(*pString==search)
			break;
		pString++;
		index++;
	}
	return index;
}```