[QUOTE=account;39786064]When searching for a max or a min, you just need to know the "current" max or min, and then check each value to see if its greater(less) than it. So in C++:
[cpp]int max, min;
max = min = array[0]; // Set them to the first element so we have something to compare to initially
for (int i = 0; i < length; i++) {
if (array[i] > max)
max = array[i];
if (array[i] < min)
min = array[i];
}[/cpp]
So we looped through the entire array, and every time we saw a value in the array that was greater (less) than the current max (min) up to that point, we made the current max (min) that value.[/QUOTE]
You rock. Never would have thought of that.
Thanks man!
[QUOTE=eternalflamez;39786117]Uh, maybe this helps:[/QUOTE] It sort of it did, I opened a new project and the copied the code into the new project. :v: But now I have errors all over the fucking place when VS didn't have a problem with it yesterday.
Well, I'm starting to code Garry's Mod 13 modules in C++ again and I discovered that the RunString method was removed. Does anyone have an idea on how I can go about calling a RunString mimic in C++?
okay, here is the project goal.
Operation pancake glutton
Record input for how many pancakes each individual person ate.
Output who ate the LEAST, and MOST, amount of pancakes
Bonus : Output what everyone ate.
Here is my code.
[SUB]
[cpp]
[/SUB]#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <Windows.h>
int main()
{
int highest, lowest;
int pancake[10];
highest = pancake[0];
cout << "Please enter the amount of pancakes each person ate. \n";
for( int count = 0; count < 10; count++)
{
cout<< "How many pancakes did person # " << count+1 << " Eat? \n";
cin>> pancake[count];
}
lowest = pancake[0];
for( int count = 0; count < 10; count++)
{
if (pancake[count] > highest)
highest = count;
if (pancake[count] < lowest)
lowest = count;
// highest works, just cout << "Person # " << highest+1 << " Ate the most!\n";
}
cout<< "Person # " << lowest+1 << " Ate the least! \n";
cout<< "Person # " << highest+1 << " Ate the most! \n";
for (int count = 0; count < 10; count++)
{
cout << "Person # " << count+1 <<" ate # "<< pancake[count] << " Pancakes! \n";
}
cin.get();
cin.get();
}
[/cpp]
PROBLEMS :::::: for some reason, it will not find who ate the least amount of pancakes.
It all looks good in my eyes, ive sat here for an hour trying to debug it but i cant find anything wrong, it just refuses to give out the lowest, if it does, it wont repeat it the next time.
Help?
[QUOTE=Septimas;39788252]okay, here is the project goal.
Operation pancake glutton
Record input for how many pancakes each individual person ate.
Output who ate the LEAST, and MOST, amount of pancakes
Bonus : Output what everyone ate.
Here is my code.
[cpp]
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <Windows.h>
int main()
{
int highest, lowest;
int pancake[10];
highest = pancake[0];
cout << "Please enter the amount of pancakes each person ate. \n";
for( int count = 0; count < 10; count++)
{
cout<< "How many pancakes did person # " << count+1 << " Eat? \n";
cin>> pancake[count];
}
lowest = pancake[0];
for( int count = 0; count < 10; count++)
{
if (pancake[count] > highest)
highest = count;
if (pancake[count] < lowest)
lowest = count;
// highest works, just cout << "Person # " << highest+1 << " Ate the most!\n";
}
cout<< "Person # " << lowest+1 << " Ate the least! \n";
cout<< "Person # " << highest+1 << " Ate the most! \n";
for (int count = 0; count < 10; count++)
{
cout << "Person # " << count+1 <<" ate # "<< pancake[count] << " Pancakes! \n";
}
cin.get();
cin.get();
}
[/cpp]
PROBLEMS :::::: for some reason, it will not find who ate the least amount of pancakes.
It all looks good in my eyes, ive sat here for an hour trying to debug it but i cant find anything wrong, it just refuses to give out the lowest, if it does, it wont repeat it the next time.
Help?[/QUOTE]
You're comparing the amount of pancakes a person ate, to the person that ate the most or least instead of how many that person ate. Try this:
[cpp]
if (pancake[count] > pancake[highest])
highest = count;
if (pancake[count] < pancake[lowest])
lowest = count;
[/cpp]
hey what's the syntax for deleting text inside a tkinter text widget? like all of the text for python 2.5.
[QUOTE=mobrockers2;39788499]You're comparing the amount of pancakes a person ate, to the person that ate the most or least instead of how many that person ate. Try this:
[cpp]
if (pancake[count] > pancake[highest])
highest = count;
if (pancake[count] < pancake[lowest])
lowest = count;
[/cpp][/QUOTE]
Im trying to get it to output the number of the person who ate the most pancakes.
I inputted your code and it just kept crashing the program for some reason.
[QUOTE=Septimas;39788252]okay, here is the project goal.
Operation pancake glutton
Record input for how many pancakes each individual person ate.
Output who ate the LEAST, and MOST, amount of pancakes
Bonus : Output what everyone ate.
Here is my code.
[SUB]
[cpp]
[/SUB]#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <Windows.h>
int main()
{
int highest, lowest;
int pancake[10];
highest = pancake[0];
cout << "Please enter the amount of pancakes each person ate. \n";
for( int count = 0; count < 10; count++)
{
cout<< "How many pancakes did person # " << count+1 << " Eat? \n";
cin>> pancake[count];
}
lowest = pancake[0];
for( int count = 0; count < 10; count++)
{
if (pancake[count] > highest)
highest = count;
if (pancake[count] < lowest)
lowest = count;
// highest works, just cout << "Person # " << highest+1 << " Ate the most!\n";
}
cout<< "Person # " << lowest+1 << " Ate the least! \n";
cout<< "Person # " << highest+1 << " Ate the most! \n";
for (int count = 0; count < 10; count++)
{
cout << "Person # " << count+1 <<" ate # "<< pancake[count] << " Pancakes! \n";
}
cin.get();
cin.get();
}
[/cpp]
PROBLEMS :::::: for some reason, it will not find who ate the least amount of pancakes.
It all looks good in my eyes, ive sat here for an hour trying to debug it but i cant find anything wrong, it just refuses to give out the lowest, if it does, it wont repeat it the next time.
Help?[/QUOTE]
mobrockers was right, your loop should look more like:
[cpp]int highest, lowest;
highest = lowest = 0; // start at 0 because they're indexes
for( int count = 0; count < 10; count++)
{
if (pancake[count] > pancake[highest])
highest = count;
if (pancake[count] < pancake[lowest])
lowest = count;
// highest works, just cout << "Person # " << highest+1 << " Ate the most!\n";
}[/cpp]
So highest and lowest are the index of the highest and lowest, so they should start at 0, not pancake[0].
Is anybody good with functional dependencies in relation to datbases?
[QUOTE=account;39788793]mobrockers was right, your loop should look more like:
[cpp]int highest, lowest;
highest = lowest = 0; // start at 0 because they're indexes
for( int count = 0; count < 10; count++)
{
if (pancake[count] > pancake[highest])
highest = count;
if (pancake[count] < pancake[lowest])
lowest = count;
// highest works, just cout << "Person # " << highest+1 << " Ate the most!\n";
}[/cpp]
So highest and lowest are the index of the highest and lowest, so they should start at 0, not pancake[0].[/QUOTE]
That works perfectly.
Can you explain what exactly i was doing wrong? I am honestly stumped right now.
Because you set it to pancake[0] not 0 (so if person 1 ate 20 pancakes it made lowest "20" and would say person "20" ate the lowest)
Also you set highest to pancake[0] before putting values into the array. Arrays in C are not 0 by default, it just so happened the junk value put into highest was probably negative.
[QUOTE=Septimas;39788882]That works perfectly.
Can you explain what exactly i was doing wrong? I am honestly stumped right now.[/QUOTE]
A few things that I noticed:
[cpp] int highest, lowest;
int pancake[10];
highest = pancake[0];[/cpp]
At this point, pancake[0] (or any element of pancake) is not initialized - it has some random value. So setting highest to an uninitialized pancake[0] is never going to be a good idea.
And aside from that, it was just your loop, which I'll comment:
[cpp]
// At first, highest and lowest are equal to pancake[0].
for( int count = 0; count < 10; count++)
{
if (pancake[count] > highest) // Looks okay for the first iteration...
highest = count; // But wait, count is the array index we're on.
if (pancake[count] < lowest) // Same thing here.
lowest = count; // On subsequent iterations, we're comparing how many pancakes someone ate to the array index - which is definitely not what we want to do.
}[/cpp]
So I just changed it to compare pancake counts with other pancake counts. highest and lowest are the indexes of the highest and lowest amount of pancakes, so we compare with pancake[highest] and pancake[lowest] instead.
I hope this helps!
[QUOTE=account;39788964]A few things that I noticed:
[cpp] int highest, lowest;
int pancake[10];
highest = pancake[0];[/cpp]
At this point, pancake[0] (or any element of pancake) is not initialized - it has some random value. So setting highest to an uninitialized pancake[0] is never going to be a good idea.
And aside from that, it was just your loop, which I'll comment:
[cpp]
// At first, highest and lowest are equal to pancake[0].
for( int count = 0; count < 10; count++)
{
if (pancake[count] > highest) // Looks okay for the first iteration...
highest = count; // But wait, count is the array index we're on.
if (pancake[count] < lowest) // Same thing here.
lowest = count; // On subsequent iterations, we're comparing how many pancakes someone ate to the array index - which is definitely not what we want to do.
}[/cpp]
So I just changed it to compare pancake counts with other pancake counts. highest and lowest are the indexes of the highest and lowest amount of pancakes, so we compare with pancake[highest] and pancake[lowest] instead.
I hope this helps![/QUOTE]
Thanks for the help man!
So to make this clear, when im comparing
highest= 0
pancake[count] > pancake[highest]
pancake [whatever variable here] would == the INPUT,
and pancake[highest] would == 0, because i made highest 0?
Im slightly confused, because pancake[0] would equal whatever the first user input, so say i put in 15 for pancake[0] , then pancake[0] would equal 15. However, comparing it to pancake[highest] is equivalent to 0??
Thanks for the help you guys. Sorry if these questions aren't up to par on what is supposed to be here. Im trying to learn and its a little hard sometimes
pancake[highest] would be 15 if highest is 0, because pancake[0] = 15 and highest is the index.
[QUOTE=account;39789113]pancake[highest] would be 15 if highest is 0, because pancake[0] = 15 and highest is the index.[/QUOTE]
OOOOOOOOOOOOOOOOOOOOOOOOOHHHHHHHHHHHHHH
Okay my brain has been mended now.
Thanks mate :)
okay what the fuck is going on
Those 3 brackets at the end of a program in VS Express, it keeps giving me errors saying to put semicolons and parenthesis at the end of them, says it's wrong and to get rid of them, then tells me to put them back when I do.
[QUOTE=Septimas;39789151]OOOOOOOOOOOOOOOOOOOOOOOOOHHHHHHHHHHHHHH
Okay my brain has been mended now.
Thanks mate :)[/QUOTE]
Make sure to use variable names you can remember. If you'd called count 'person' instead you might not have been confused as then you'd clearly see you're comparing pancakes to people while what you wanted was to compare pancakes to pancakes.
[editline]4th March 2013[/editline]
[QUOTE=LtKyle2;39789250]okay what the fuck is going on
Those 3 brackets at the end of a program in VS Express, it keeps giving me errors saying to put semicolons and parenthesis at the end of them, says it's wrong and to get rid of them, then tells me to put them back when I do.[/QUOTE]
See if you haven't forgotten any brackets, parentheses or semicolons earlier on.
[QUOTE=LtKyle2;39787356]It sort of it did, I opened a new project and the copied the code into the new project. :v: But now I have errors all over the fucking place when VS didn't have a problem with it yesterday.[/QUOTE]
If you use multiple classes, or copied the wrong code, VS will give errors about your namespace.
Usually when you have to copy paste code from one project to another, this helps:
-Make the same class objects as in the original
-Leave the auto-generated code, only copy paste around it. (By which I mean the namespace and the constructor, you should also copy the usings if you need them)
When using C#, OpenTK and the stencil test on my computer, or my friend's, it all works just the way it should.
However, when I try it on my girlfriend's laptop, or my father's it doesn't cut anything and just leaves a giant rectangle in which it cuts fine on my pc.
So I've got 2 computer's it works on and 2 it doesn't work on.
Is there a way to test if the hardware support stencil tests?
I'm learning to code, and as a project I've been thinking about making a program that can log on to my student page, access each lesson for tomorrow, copy the notes and homework into a notepad document, and save it on my desktop.
Now, I know this is probably out of my league, but I want to see how far I can come. But I have no idea how to make a program to access and copy information from a webpage (some kind of bot?). Can you guys give me any help on where to start? Are there programs that I can use to make it all easier? Or what will I need?
Id say use c# and implement java into it to grab the results and click(); etc shit.
[QUOTE=xThaWolfx;39797092]When using C#, OpenTK and the stencil test on my computer, or my friend's, it all works just the way it should.
However, when I try it on my girlfriend's laptop, or my father's it doesn't cut anything and just leaves a giant rectangle in which it cuts fine on my pc.
So I've got 2 computer's it works on and 2 it doesn't work on.
Is there a way to test if the hardware support stencil tests?[/QUOTE]
Stencil testing has been part of OpenGL since 1.0 (1992), the hardware will support it. There's probably a difference between implementations or maybe your girlfriend's and dad's laptops aren't creating a stencil buffer by default or something. Test things on those laptops and try changing minor things until it works on both.
[QUOTE=Septimas;39797235]Id say use c# and implement java into it to grab the results and click(); etc shit.[/QUOTE]
Well, I haven't begun learning C# yet, I've mainly been using Java and Python. Should I dig into it, or would you suggest another way?
finished my story generator and handed in, everything works yay
[QUOTE=cdot;39797437]Well, I haven't begun learning C# yet, I've mainly been using Java and Python. Should I dig into it, or would you suggest another way?[/QUOTE]
Most bots i used to use back in the day involved a simple com browser in c# and implemented a lot of java features. Not sure, i don't know much about the subject of making them.
I did however get a free xbox 360, wii, and other tech shit with it (;
[URL=http://minus.com/lilvUA6PO49nr][IMG]http://i.minus.com/jilvUA6PO49nr.png[/IMG][/URL]
here it is, after asking for help a few times, bam.
[QUOTE=Septimas;39798379]Most bots i used to use back in the day involved a simple com browser in c# and implemented a lot of java features. Not sure, i don't know much about the subject of making them.
I did however get a free xbox 360, wii, and other tech shit with it (;[/QUOTE]
Thanks, I'll check it out!
Alright guys, I'm drawing a blank here. I have extensive experience with C++ and am now working through a series of personal projects in C. Right now, I'm writing a shell.
For instructive purposes, I am having the program manage its own memory, i.e. I allocate a block of unsigned static chars at the global scope and make functions that parcel out blocks of 64 chars from there by passing void*'s. I have this so far.
[cpp]
////////////////////////////////////////////////////////////////////////////////
//
// Memory Allocation
// Info: Currently uses 8192 chars to store strings; this yields 128 "blocks" of
// size 64 chars each. The program will parcel out 1 block at a time via
// pointers and manage fragmentation, clearing etc.
//
////////////////////////////////////////////////////////////////////////////////
unsigned static char mem[MEMORY_SIZE]; // Main memory block for the program
size_t memSize; // How much of mem is used
// Array of booleans showing which blocks of memory are free; a block is free if
// its index in memIsFree is true and false if it is occupied
bool memIsFree[MEM_MAP_SIZE];
...
////////////////////////////////////////////////////////////////////////////////
// setupFreeMemoryMap: Fills the memIsFree array with true values to start with
// since no functions have yet requested memory
////////////////////////////////////////////////////////////////////////////////
void setupFreeMemoryMap()
{
int i; // Loop variable
for (i = 0; i < MEM_MAP_SIZE; i++)
memIsFree[i] = true;
};
////////////////////////////////////////////////////////////////////////////////
// mcshMalloc: Returns a pointer to an address in mem that represents allocated
// memory space
// * <size> requested size of memory to allocate
////////////////////////////////////////////////////////////////////////////////
void* mcshMalloc(size_t size)
{
int index; // Used to store memory search results
void* address; // Return value of address in mem
// DEBUG
printf("mcsh> Requesting block for mem of size %d.\n", size);
index = memSearch(); // Get index of first unused block in mem
address = &mem[MEM_MAP_SIZE * index]; // Grab the address of the block
// Check if the block goes out of bounds of memory
if (memSize + size > MEMORY_SIZE || (int)address == MEMORY_FULL)
{
popError("Out of memory.");
return (void*)MEMORY_FULL; // There's no more memory to give out
}
else // Otherwise return the block address
{
memIsFree[index] = false;
memSize += size;
// DEBUG
printf("mcsh> Allocated block %d. New size: %d\n", index, memSize);
return address;
}
};
////////////////////////////////////////////////////////////////////////////////
// memSearch: Returns the index of the first block in mem that is free for
// allocation
////////////////////////////////////////////////////////////////////////////////
int memSearch()
{
int i; // Loop variable
for (i = 0; i < MEM_MAP_SIZE; i++)
{
if (memIsFree[i]) return i;
}
return MEMORY_FULL; // No free blocks were found
};
////////////////////////////////////////////////////////////////////////////////
// mcshFree: Frees the specified mem block; we don't actually need to delete the
// data there, because the next request will simply over-write it. The bool
// array works like a dirty bit.
// * <index> index of the block to be freed
////////////////////////////////////////////////////////////////////////////////
void mcshFree(int index)
{
memIsFree[index] = true;
memSize -= STRING_SIZE;
// DEBUG
printf("mcsh> Freed block %d.\n", index);
};
[/cpp]
I have functions that properly allocate the memory blocks (64 chars) to functions that request it, and the DEBUG printf's support my theory that it's working correctly.
However, I need a way to free these blocks. Right now I'm just treating the boolean map like an array of dirty bits; if it's boolean is set to true, then other data can overwrite it. I don't bother actually clearing out the memory yet.
My question is, how do I get the address of the variable I allocated after assigning it like so:
char* exampleString = mcshMalloc(STRING_SIZE); // Where STRING_SIZE is 64, for 64 chars
Obviously, exampleString is pointing to a location in mem[] that I passed it. Now, I need to get the address it's pointing to so that I can set it's boolean value for isFree back to true. How do?
I know this is a simple solution and I'm just having a brain-fart.
[cpp](exampleString - mem) / 64[/cpp]
I believe that would give you the block it's in.
You can also do a right shift for faster division :D
[cpp](exampleString - mem) >> 6[/cpp]
[QUOTE=account;39799537][cpp](exampleString - mem) / 64[/cpp]
I believe that would give you the block it's in.
You can also do a right shift for faster division :D
[cpp](exampleString - mem) >> 6[/cpp][/QUOTE]
That format didn't work, unfortunately.
The compiler error was: invalid operands to binary - (have char * and unsigned char *)
So I tried
[cpp]
int blockIndex = ((*exampleString - *mem) / 64);
mcshFree(blockIndex);
[/cpp]
It didn't work, unfortunately. I tested it on a pack of 7 strings I allocated, and it freed block 1, 1, 0, 0, 0, 0, 1 in that order.
Here' my thought process. My code for allocating is like this:
[cpp]
address = &mem[MEM_MAP_SIZE * index]; // Grab the address of the block
...
return address;
[/cpp]
Where MEM_MAP_SIZE is 128 blocks (128 blocks of 64 chars). Which translates (in my mind) to:
[code]
blockAddress = addressof(mem + (128 * index))
[/code]
therefore, index SHOULD be
[code]
index = (blockAddress - mem) / 128
[/code]
by algrebra.
Sorry, you need to Log In to post a reply to this thread.