You're never checking if 2 is larger/smaller than anything.
[QUOTE=Tamschi;42433260]In short, don't initialize variables that don't have a sensible default value, unless it's absolutely necessary.[/QUOTE]
[QUOTE=bord2tears;42425299]Your only assignments to largest are made inside "if" statements. Even though it one of them may always be true (the two statements may be mutually exclusive) the compiler isn't smart enough to know that.
[B]You'll need to initialize "largest" with some default value[/B] or only print inside those two if statements, with an else for "something went wrong".[/QUOTE]
[editline]6th October 2013[/editline]
[QUOTE=robmaister12;42433265]You're never checking if 2 is larger/smaller than anything.[/QUOTE]
[code]
//if statements to find out which number is largest
if (userInputInt > userInputInt2)
{
largest = userInputInt;
}
[/code]
[code]
//if statements to find out which number is smallest
if (userInputInt < userInputInt2)
{
smallest = userInputInt;
}
[/code]
?
[QUOTE=garychencool;42433644][editline]6th October 2013[/editline]
[code]
//if statements to find out which number is largest
if (userInputInt > userInputInt2)
{
largest = userInputInt;
}
[/code]
[code]
//if statements to find out which number is smallest
if (userInputInt < userInputInt2)
{
smallest = userInputInt;
}
[/code]
?[/QUOTE]
No, that's 1 > 2, not 2 > 1 or 2 > 3. Run through the code in your head:
largest is 0.
is the first one greater than the second one? no
largest is 0.
is the third one greater than the first one? no
largest is 0.
smallest is 0.
is the first one smaller than the second one? yes
smallest is 564656.
is the third one smaller than the first one? yes
smallest is -1545456.
[editline]6th October 2013[/editline]
Here's another way to look at it. The second one can never be chosen as the largest/smallest since this isn't anywhere in your code:
[code]largest = userInputInt2;[/code]
[QUOTE=robmaister12;42434531]
Here's another way to look at it. The second one can never be chosen as the largest/smallest since this isn't anywhere in your code:
[code]largest = userInputInt2;[/code][/QUOTE]
Ohhhh, I see now. Thanks!
An easier solution is to compare every entered int with the largest/smallest. Rather than with every other int.
ex
[cpp]
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int largest = a;
int smallest = a;
if ( b > largest ) { largest = b; }
if ( c > largest ) { largest = c; }
if ( b < smallest ) { smallest = b; }
if ( c < smallest) { smallest = c; }
System.out.printf("The sum is %d, the average is %f, the product is %d, the largest is %d, the smallest is %d",
a+b+c,
double(a+b+c)/3.0,
(a*b*c),
largest,
smallest);[/cpp]
Personally I'd add them all to an array of sorts, then loop through and check, instead of making 20 if statements for each scenario.
[QUOTE=eternalflamez;42440938]Personally I'd add them all to an array of sorts, then loop through and check, instead of making 20 if statements for each scenario.[/QUOTE]
Yeah I would use arrays but in the comp sci class, we haven't learned about it and we are just using lots of if statements.
[QUOTE=garychencool;42442184]Yeah I would use arrays but in the comp sci class, we haven't learned about it and we are just using lots of if statements.[/QUOTE]
In my class my professor marks us down if we use anything he hasn't taught. We're way into the lessons and we haven't even learned loops yet :/.
College is so slow eeee
[QUOTE=Naelstrom;42443351]In my class my professor marks us down if we use anything he hasn't taught. We're way into the lessons and we haven't even learned loops yet :/.
College is so slow eeee[/QUOTE]
Is that actually a thing, that's like encouraging people not to try.
So what do you do? Play video games? Sounds like my computer tech class.
My laptop kinda sucks, so I play Brutal Doom the whole time. :u
[editline]7th October 2013[/editline]
[QUOTE=Johnny Guitar;42443423]Is that actually a thing, that's like encouraging people not to try.[/QUOTE]
The way he puts it is "you're given a set of tools and a problem. You must solve the problem with your limited tools."
and
"It's unfair for other students if you go 'up and beyond'".
Linker errors are worst kind of error. I have no idea how to start on them.
First time seeing Templates in C++ today. Had to write LinkedList class, compiles fine but the linker freaks out.
[QUOTE]1>TestLinkedList.obj : error LNK2019: unresolved external symbol "public: __thiscall LinkedList<int>::LinkedList<int>(void)" (??0?$LinkedList@H@@QAE@XZ) referenced in function "public: __thiscall TestLinkedList::TestLinkedList(void)" (??0TestLinkedList@@QAE@XZ)
1>TestLinkedList.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList<int>::Add(int const &)" (?Add@?$LinkedList@H@@QAEXABH@Z) referenced in function "public: virtual void __thiscall TestLinkedList::GamePaint(struct tagRECT)" (?GamePaint@TestLinkedList@@UAEXUtagRECT@@@Z)
1>TestLinkedList.obj : error LNK2019: unresolved external symbol "public: int __thiscall LinkedList<int>::GetLength(void)const " (?GetLength@?$LinkedList@H@@QBEHXZ) referenced in function "private: void __thiscall TestLinkedList::Display(class LinkedList<int> const &)" (?Display@TestLinkedList@@AAEXABV?$LinkedList@H@@@Z)
1>TestLinkedList.obj : error LNK2019: unresolved external symbol "public: int const & __thiscall LinkedList<int>::operator[](int)const " (??A?$LinkedList@H@@QBEABHH@Z) referenced in function "private: void __thiscall TestLinkedList::Display(class LinkedList<int> const &)" (?Display@TestLinkedList@@AAEXABV?$LinkedList@H@@@Z)
1>TestLinkedList.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList<int>::Insert(int const &,int)" (?Insert@?$LinkedList@H@@QAEXABHH@Z) referenced in function "public: virtual void __thiscall TestLinkedList::GamePaint(struct tagRECT)" (?GamePaint@TestLinkedList@@UAEXUtagRECT@@@Z)
1>TestLinkedList.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList<int>::Erase(int)" (?Erase@?$LinkedList@H@@QAEXH@Z) referenced in function "public: virtual void __thiscall TestLinkedList::GamePaint(struct tagRECT)" (?GamePaint@TestLinkedList@@UAEXUtagRECT@@@Z)
1>TestLinkedList.obj : error LNK2019: unresolved external symbol "public: void __thiscall LinkedList<int>::Clear(void)" (?Clear@?$LinkedList@H@@QAEXXZ) referenced in function "public: virtual void __thiscall TestLinkedList::GamePaint(struct tagRECT)" (?GamePaint@TestLinkedList@@UAEXUtagRECT@@@Z)[/QUOTE]
Header file
[CPP]
template<typename T>
struct LinkedListNode
{
LinkedListNode *m_NextPtr;
T m_Value;
LinkedListNode(const T& value, LinkedListNode* nextPtr = nullptr);
};
template<typename T>
class LinkedList
{
public:
LinkedList();
~LinkedList();
void Add(const T& value);
int GetLength() const;
T& operator[](int index);
const T& operator[](int index) const;
void Insert(const T& value, int index);
void Erase(int index);
void Clear();
private:
T *m_RootPtr;
};
[/CPP]
CPP file
[CPP]
#include "LinkedList.h"
#include "GameDefines.h"
//--------------------
// LinkedListNode Struct
//--------------------
template<typename T>
LinkedListNode<T>::LinkedListNode(const T& value, LinkedListNode* nextPtr)
:m_NextPtr(nextPtr)
,m_Value(value)
{
}
//--------------------
// LinkedList Class
//--------------------
template<typename T>
LinkedList<T>::LinkedList()
:m_RootPtr(nullptr)
{
}
template<typename T>
LinkedList<T>::~LinkedList()
{
Clear();
}
template<typename T>
void LinkedList<T>::Add(const T& value)
{
if (m_RootPtr == nullptr)
{
m_RootPtr = new LinkedListNode(value);
}
els
{
LinkedListNode *itPtr = m_RootPtr;
while (itPtr->m_NextPtr != nullptr) itPtr = itPtr->m_NextPtr;
itPtr->m_NextPtr = new LinkedListNode(value);
}
}
template<typename T>
int LinkedList<T>::GetLength() const
{
LinkedListNode *pointer = m_RootPtr;
int counter = 0;
while (pointer != nullptr)
{
pointer = pointer->m_NextPtr;
++counter;
}
return counter;
}
template<typename T>
T& LinkedList<T>::operator[](int index)
{
LinkedListNode *itPtr = m_RootPtr;
for (int i = 0; i < index; ++i)
{
itPtr = itPtr->m_NextPtr;
}
return itPtr->m_Value;
}
template<typename T>
const T& LinkedList<T>::operator[](int index) const
{
LinkedListNode *itPtr = m_RootPtr;
for (int i = 0; i < index; ++i)
{
itPtr = itPtr->m_NextPtr;
}
return itPtr->m_Value;
}
template<typename T>
void LinkedList<T>::Insert(const T& value, int index)
{
LinkedListNode *itPtr = m_RootPtr;
for (int i = 0; i < index - 1; ++i)
{
itPtr = itPtr->m_NextPtr;
}
itPtr->m_NextPtr = new LinkedListNode(value, itPtr->m_NextPtr);
}
template<typename T>
void LinkedList<T>::Erase(int index)
{
LinkedListNode *itPtr, *tempPtr;
itPtr = m_RootPtr;
for (int i = 0; i < index - 1; ++i)
{
itPtr = itPtr->m_NextPtr;
}
tempPtr = itPtr->m_NextPtr->m_NextPtr;
delete itPtr->m_NextPtr;
itPtr->m_NextPtr = tempPtr;
}
template<typename T>
void LinkedList<T>::Clear()
{
LinkedListNode *itPtr, *tempPtr;
itPtr = m_RootPtr;
while (itPtr != nullptr)
{
tempPtr = itPtr->m_NextPtr;
delete itPtr;
itPtr = tempPtr;
}
m_RootPtr = nullptr;
}
[/CPP]
please help me before i pull out all my hair.
Templates are instantiated at compile-time, which means the implementations must be visible when you use it.
Solution: put the implementation in the header-file, or #include the implementation-file in the header. I generally do the latter, but I use a different file-extension than .cpp. Common ones are .ipp or .inl and I've also seen .impl and .tmpl.
[editline]7th October 2013[/editline]
Or you could also instantiate the class in the implementation file for certain types, although it won't be as flexible of course.
You can do this by putting [code]template class LinkedList<int>;[/code] in the implementation file.
I have little experience with C# but I'm using it for a project recently, the issue I've come up with is this;
I am creating a grid of pictureboxes, using a for loop just creating, setting the position and other key properties, then adding it to the forms controls.
The problem is, I need an event the be called when these are clicked, the pictureboxes name is always 'square' so I tried creating a generic square_Clicked method, however it is never called
From what I've researched I need some sort of implementation of delegates, I have no idea how to actually do so or if there is an easier way.
The Picturebox stores [url=http://msdn.microsoft.com/en-us/library/ms173171.aspx]delegate[/url]s. They're basically variables of a function-type. The picture can then store a reference to the function to be called when it is clicked.
In the loop where you create them, add
[code]square.Click += square_Clicked[/code]
The signature of square_Clicked must be [code]void square_Clicked(object, System.EventArgs)[/code]
wow, thanks a lot man, that worked out just fine, thanks a lot
[editline]7th October 2013[/editline]
current code:
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace tileSheet
{
public partial class Form1 : Form
{
public Form1()
{
PictureBox[] square = new PictureBox[500];
for (int i = 0; (i * 20) < this.Width; i++)
{
//Control[] square;
square[i] = new PictureBox();
InitializeComponent();
//PictureBox square[i] = new PictureBox();
square[i].Size = new Size(20, 20);
square[i].Location = new Point(20 * i, 0);
square[i].BackColor = Color.Red;
square[i].Click += new System.EventHandler(square_Clicked);
this.Controls.Add(square[i]);
}
}
public void square_Clicked(object square, System.EventArgs e)
{
label1.Text = "test";
}
}
}[/code]
I get no syntax errors but square_Clicked is never called
I tried doing just
[code]square.Click += square_Clicked[/code]
that did no different as well
any help?
Can someone explain WPF's data bindings to me and how to use them? I know that they are some fancy way to bind values and stuff to something so you don't have to make some code to see stuff, but MSDN's documentation ([URL="http://msdn.microsoft.com/en-us/library/ms752347.aspx"]link link[/URL]) is not easily understood. MSDN's [URL="http://i.msdn.microsoft.com/dynimg/IC38064.png"]sample program[/URL] is actually just what I want. A list of objects, each with different properties, and someplace to see the properties of the currently selected object (though I have a TreeView instead of a normal list). Thanks :)
[QUOTE=ruarai;42451141]Why are you making so big of an array of picture boxes? I hope you aren't thinking that this is how you do tile-based stuff in windows forms, because if so you should look up GDI.[/QUOTE]
Also InitializeCompontent should only be called once.
I'm looking for a simple way to parse command line arguments.
So this doesn't work?
[code]
if(args[i] == "-resourcetxtpath")
{
resourcetxt = args[++i];
}
if(args[i] == "-sysresource")
{
ramoutput = args[++i];
}
[/code]
lol..
Forgive me in advance for being a total noob, just seeing if anyone can help me with my school shit.
Anyone familiar with basic pep8 programming? I have an Intro to Computing class and I need to write out my name using just a line of hexidecimal numbers. I'm pretty lost.
Here's what I'm working with. I know that I need to input hex into the object code box, but I'm not sure where to start.
[IMG]http://i.imgur.com/tXkU4ab.png[/IMG]
Just learn up some Hexadecimal syntax. Not sure how to implement it into your program, or pep8.
[url]http://www.codeproject.com/Articles/4069/Learning-Binary-and-Hexadecimal[/url]
I read some. It's not bad.
I know the basics of hex, I just don't know how I'm supposed to use it as an input for this program.
For example, the phrase "50 00 48 50 00 65 50 00 6C 50 00 6C 50 00 6F 00 zz" is supposed to create an output of "Hello". I just have no idea how this works.
Aah, ok
[QUOTE=Duze;42463335]I'm looking for a simple way to parse command line arguments.
So this doesn't work?
[code]
if(args[i] == "-resourcetxtpath")
{
resourcetxt = args[++i];
}
if(args[i] == "-sysresource")
{
ramoutput = args[++i];
}
[/code]
lol..[/QUOTE]
That would work for the first argument given that i is initialized with 1 and you're not using C or C++ (because direct comparison of c-strings - pointers).
args[0] contains the program name and it'd only work for the first argument because once it is parsed, you only increment i once, meaning it stays at the value-part of the -key value pair.
You're also depending on the correct order, which can be solved by using a loop and you can easily access args out-of-bounds, which would also be solved with a loop like
[code]for(int i = 1; i < argc; ++i)[/code]
[QUOTE=WhiskeyTangoF;42463409]Forgive me in advance for being a total noob, just seeing if anyone can help me with my school shit.
Anyone familiar with basic pep8 programming? I have an Intro to Computing class and I need to write out my name using just a line of hexidecimal numbers. I'm pretty lost.
Here's what I'm working with. I know that I need to input hex into the object code box, but I'm not sure where to start.
[IMG]http://i.imgur.com/tXkU4ab.png[/IMG][/QUOTE]
I don't know that environment, but try putting it in the [url=https://en.wikipedia.org/wiki/Object_file]object code[/code]-field.
Having some trouble with Mono.Cecil:
[code] var mainType = module.Types.Single(t => t.Name == "Main");
var gameType = typeof(xna.Game);
var managerType = typeof(xna.Content.ContentManager);
var effectType = typeof(xna.Graphics.Effect);
var batchType = typeof(xna.Graphics.SpriteBatch);
var blendType = typeof(xna.Graphics.BlendState);
var getContentMethod = module.Import(gameType.GetMethod("get_Content", new Type[] { }));
var loadContentMethod = module.Import(managerType.GetMethods().Single(m => m.Name == "Load").MakeGenericMethod(effectType));
var beginMethod = module.Import(batchType.GetMethod("Begin",
new Type[] { typeof(xna.Graphics.SpriteSortMode), typeof(xna.Graphics.BlendState),
typeof(xna.Graphics.SamplerState), typeof(xna.Graphics.DepthStencilState),
typeof(xna.Graphics.RasterizerState), typeof(xna.Graphics.Effect) }));
var effectField = new FieldDefinition("shaderEffect",
Mono.Cecil.FieldAttributes.Public | Mono.Cecil.FieldAttributes.Static,
module.Import(effectType));
var staticBlendField = module.Import(blendType.GetField("AlphaBlend"));
mainType.Fields.Add(effectField);
var loadMethod = mainType.Methods.Single(m => m.Name == "LoadContent");
var loadProc = loadMethod.Body.GetILProcessor();
var loadReturn = loadMethod.Body.Instructions.Last(i => i.OpCode == OpCodes.Ret);
loadProc.InsertBefore(loadReturn, loadProc.Create(OpCodes.Ldarg_0));
loadProc.InsertBefore(loadReturn, loadProc.Create(OpCodes.Ldarg_0));
loadProc.InsertBefore(loadReturn, loadProc.Create(OpCodes.Call, getContentMethod));
loadProc.InsertBefore(loadReturn, loadProc.Create(OpCodes.Ldstr, "shader"));
loadProc.InsertBefore(loadReturn, loadProc.Create(OpCodes.Callvirt, loadContentMethod));
loadProc.InsertBefore(loadReturn, loadProc.Create(OpCodes.Stfld, effectField));
var renderMethod = mainType.Methods.Single(m => m.Name == "RenderTiles");
var renderProc = renderMethod.Body.GetILProcessor();
var renderBegin = renderMethod.Body.Instructions.Single(i =>
i.Operand != null && i.Operand.ToString().Contains("SpriteBatch::Begin"));
var newBegin = renderProc.Create(OpCodes.Callvirt, beginMethod);
renderProc.Replace(renderBegin, newBegin);
renderProc.InsertBefore(newBegin, renderProc.Create(OpCodes.Ldc_I4_0));
renderProc.InsertBefore(newBegin, renderProc.Create(OpCodes.Ldsfld, staticBlendField));
renderProc.InsertBefore(newBegin, renderProc.Create(OpCodes.Ldnull));
renderProc.InsertBefore(newBegin, renderProc.Create(OpCodes.Ldnull));
renderProc.InsertBefore(newBegin, renderProc.Create(OpCodes.Ldnull));
renderProc.InsertBefore(newBegin, renderProc.Create(OpCodes.Ldarg_0));
renderProc.InsertBefore(newBegin, renderProc.Create(OpCodes.Ldsfld, effectField));[/code]
The code seems to generate IL nearly identical to the test code I based it on, but the JIT compiler fails when running the second method I edit.
Reflector also failed, saying an ambiguous match was found, which makes me think it's a reference/import problem.
[editline]9th October 2013[/editline]
It's meant to add a shaderEffect = Content.Load<Effect>(name); to LoadContent and replace the spriteBatch.Begin call in RenderTiles.
Does anyone know what could be causing this :
[img]https://dl.dropboxusercontent.com/u/28926055/bug.png[/img]
Thought it was because I didn't enable depth testing but enabling it and adding it to the glClear command didn't seem to work.
[QUOTE=quincy18;42465393]Does anyone know what could be causing this :
[img]https://dl.dropboxusercontent.com/u/28926055/bug.png[/img]
Thought it was because I didn't enable depth testing but enabling it and adding it to the glClear command didn't seem to work.[/QUOTE]
Link's broken.
[QUOTE=Perl;42465585]Link's broken.[/QUOTE]
Random dropbox delete :S Anyway used dropbox to restore the pic sorry :/
I've been reading about virtual memory space and this shit's confusing as hell.
First question, what decides where these variables are stored in the virtual memory?
Every time the executable loads, the variable aVar gets loaded into different part of the memory, why is that?
[IMG]http://puu.sh/4LR1k.png[/IMG]
And second question: What happens to the memory after I delete an object?
It apparently doesn't clear the memory,because you can still access some random data through the pointer.
The red ones are the bytes that changed after I deleted the object, but they are not cleared to 0.
If they are freed why can I still see them in my program's virtual memory space?
[IMG]http://puu.sh/4LS1L.png[/IMG]
[QUOTE=ollie;42468675]First question, what decides where these variables are stored in the virtual memory?
Every time the executable loads, the variable aVar gets loaded into different part of the memory, why is that?[/QUOTE]
Roughly, the operating system.
More precisely a combination of the (executable) loader (which is part of most operating systems) and the binary (for example static memory (.bss) will always be at the same [i]offset[/i]).
You were already talking about virtual memory, so keep in mind that the addresses do not necessarily correspond to the (actual) physical address.
[quote]And second question: What happens to the memory after I delete an object?
It apparently doesn't clear the memory,because you can still access some random data through the pointer.[/quote]
Well, the C++ standard simply states something among the lines of that attempts to access the memory after deleting it result in undefined behavior.
Most implementations will do something like telling the OS that it's free to reassign the memory again or keep it around for a while and "cache" the memory, so if you need some again it can reuse the memory that was already allocated once before.
Sorry, you need to Log In to post a reply to this thread.