28 Replies - 3170 Views - Last Post: 30 April 2011 - 06:05 AM
#1
[C#] Create Array on summing some variables
Posted 25 April 2011 - 01:12 PM
I am new to C# and have searched a long time for a solution but can't find it... I hope you could help me.
I have many variables (inserted by the user of the program, when run) with different values. Now my program should be able to group them automatically into arrays by summing them together.
I'll explain better...
The sum of the variables in the arrays should be everytime <= 100 (hundred or smaller). Let's say I add var1 = 20, var2 = 75, var3 = 99, var4 = 35, var5 = 1, var6 = 2, var7 = 65..... and so on. Now the program creates automatically some arrays adding theese vars. For example "ARRAY1" contains var1, var2, var5, var6 (Sum = 98) || "ARRAY2" contains var3 (Sum = 99) || "ARRAY3" contains var4, var7 (Sum = 100).
I hope you understand what I would like to do. How can be done something like that?
Thank you in advance and sorry for my bad english.
Replies To: [C#] Create Array on summing some variables
#2
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 01:20 PM
int total = 0;
foreach (int yogi in myIntArray)
{
total += yogi;
}
This would be covered in the first chapter about arrays, of any intro book.
Standard resources, references and suggestions for new programmers.
I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.
But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through
Free editions of Visual Studio 2010
May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.
D.I.C. C# Resource page Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading
The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes
Working with environmental variables
Debugging tutorial
Debugging tips
Great debugging tips
Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.
C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.
Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.
Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.
These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio
Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.
Let me also throw in a couple tips:
- You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
- Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e) - You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on
#3
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 01:53 PM
Anyway I must say, that I know this function "foreach". But this is not my problem, how to loop trough my data and sum the vars together. My big problem is: how can I loop trough my vars and add them in the arrays in order to reach everytime a total of MAXIMUM 100. Important: the vars must not be splitted!! They do not have to be sorted in any manner, but they should be "grouped" as I have explained above. I really do not have an idea how I could do that.
PS. I have a book called "Visual C# 2008" and I have started to read it even if I prefer following tutorials found on the web...
This post has been edited by Anthonidas: 25 April 2011 - 01:56 PM
#4
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 02:02 PM
You don't easily take squiggle1, squiggle2, squiggle3 and just loop through them to add them to an array.
There are some advanced ways to get the variables using reflection, but honest I feel those are just a way to work around bad code design.
You can do this
myArray[0] = squiggle0; myArray[1] = squiggle1; myArray[99] = squiggle99;
Instead of hard coding a group of variables as squiggle1... squigle99 is there some reason you can't have the data put into array elements from the beginning?
#5
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 02:17 PM
tlhIn`toq, on 25 April 2011 - 02:02 PM, said:
You don't easily take squiggle1, squiggle2, squiggle3 and just loop through them to add them to an array.
There are some advanced ways to get the variables using reflection, but honest I feel those are just a way to work around bad code design.
You can do this
myArray[0] = squiggle0; myArray[1] = squiggle1; myArray[99] = squiggle99;
Instead of hard coding a group of variables as squiggle1... squigle99 is there some reason you can't have the data put into array elements from the beginning?
yep, there is an important one: I just don't know the values from beginning! as already said, the variables are entered from the user. that's why i can't do it before...
#6
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 02:21 PM
Let's be clear on terms:
Variables you create in code.
Values the user enters in at runtime.
The user is NOT entering variables. The user IS entering values.
You had to make the variables in code.
If you made the variable var1, var2, var3 then you have the ability to add them to an array. What the user enters as the values to those variables doesn't matter.
Let's do this... show us the code where the user is entering the values.
This post has been edited by tlhIn`toq: 25 April 2011 - 02:21 PM
#7
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 02:38 PM
List<int[]> batches = new List<int[]>();
int[] myArray = new int[] { 10,23,34,23,52,26,23,24,61,12,23,51,41,12,12,34,62,21,0,21,21,52,31,20,49,1};
int currentSum = 0;
int lastIndex = 0;
int[] batch;
int index = 0;
for (int i =0; i<myArray.Length;i++)
{
index = 0;
int sum = currentSum + myArray[i];
if (sum <= 100)
{
currentSum += myArray[i];
}
else if (sum > 100)
{
currentSum = myArray[i];
batch = new int[i-lastIndex];
for (int z = lastIndex; z < i; z++)
{
batch[index] = myArray[z];
index++;
}
batches.Add(batch);
lastIndex = i;
}
if (i == myArray.Length - 1)
{
batch = new int[myArray.Length - lastIndex];
for (int z = lastIndex; z < myArray.Length; z++)
{
batch[index] = myArray[z];
index++;
}
batches.Add(batch);
}
}
Threw it together in five minutes. Optimizations in progress - I will update it as soon as I finish those, but you should get the idea. I am using a dummy int array that can be replaced with what the user entered.
#8
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 02:49 PM
First of all, you have to keep the arrays somewhere as you dinamically create them. This means you have to use a list of arrays
List<int[]> myListOfArrays = new List<int[]>(); myList.Add(new int[100]); //the worst case you will have 100 items in your array
Then, each time the user introduce a number you check through your list of arrays if you have space to add it or you need to create a new one
//for each value user introduces
bool bAdded = false;
foreach (int[] someArray in myListOfArrays)
{
if (someArray.Sum() + read_value <= 100)
{
someArray[someArray.Count<int>(p => p != 0)] = read_value; //suppose 0 has to be ignored
bAdded = true;
break;
}
}
if (!bAdded)
{//create a new array and add it, add the read_value to it and add the array to the list }
#9
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 02:53 PM
ok i explain you what i would like to do. perhaps it's easyer...
i would like to create a tool for a game database.
in this game you have different maps. in each map you have different items. every item has a percentage of how often it appears in the map. now the problem is,that the emulator of the game is not able to select all items together. that's why i have to make groups of the items. and as i said before, the groups percentage must not be higher than 100. that means,that if you have 3 items for example, item1 = 50%, item2 = 60% and item3 = 30% in the same map, you can not put them all in the same group as the total would be 140%.
now my tool has only 3 variables: MapID, ItemID, Percent. the user enters theese three values for every item he would like to have. when he is done, this tool should calculate the percentages and make the groups so that everything can be entered in a database.
do you have suggestions? it's quite complicated, i know... but i have to do it.
and i hope you got what i tryed to explain
#10
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 02:56 PM
You are both missing the major part of the OP's situation.
He understands how to total an array. Not to mention I already showed him that.
He says he doesn't have an array yet.
He says he has a bunch of individual variables that he somehow wants to turn into an array to then loop through.
Quote
He doesn't have
var[0] - var[99]
he has
var1, var2, var3, var4, var5, ..., var98, var99
There are some language issues with OP as he says the user is entering the variables, which isn't right. The user is entering values.
That is why I have asked the OP for the actual code he uses to get the values from the user input; so we can actually see if he really does have a bunch of individual variables.
#11
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 03:04 PM
Quote
#12
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 03:11 PM
Core, on 25 April 2011 - 04:04 PM, said:
tlhIn`toq, on 25 April 2011 - 03:02 PM, said:
You don't easily take squiggle1, squiggle2, squiggle3 and just loop through them to add them to an array.
There are some advanced ways to get the variables using reflection, but honest I feel those are just a way to work around bad code design.
You can do this
myArray[0] = squiggle0; myArray[1] = squiggle1; myArray[99] = squiggle99;
Instead of hard coding a group of variables as squiggle1... squigle99 is there some reason you can't have the data put into array elements from the beginning?
Glad to see you've caught up with the conversation, Core.
This post has been edited by tlhIn`toq: 25 April 2011 - 03:12 PM
#13
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 03:13 PM
Core, on 25 April 2011 - 03:04 PM, said:
i think you got it... but i'm not sure if you have seen this
Anthonidas, on 25 April 2011 - 02:53 PM, said:
ok i explain you what i would like to do. perhaps it's easyer...
i would like to create a tool for a game database.
in this game you have different maps. in each map you have different items. every item has a percentage of how often it appears in the map. now the problem is,that the emulator of the game is not able to select all items together. that's why i have to make groups of the items. and as i said before, the groups percentage must not be higher than 100. that means,that if you have 3 items for example, item1 = 50%, item2 = 60% and item3 = 30% in the same map, you can not put them all in the same group as the total would be 140%.
now my tool has only 3 variables: MapID, ItemID, Percent. the user enters theese three values for every item he would like to have. when he is done, this tool should calculate the percentages and make the groups so that everything can be entered in a database.
do you have suggestions? it's quite complicated, i know... but i have to do it.
and i hope you got what i tryed to explain
This post has been edited by Anthonidas: 25 April 2011 - 03:15 PM
#14
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 03:22 PM
I was trying to lead him toward better architecture, and to show us that he actually has made an effort to write his own code.
Anthonidas: Have you actually written any code for this homework assignment?
It sounds like your real question is:
"How do I build a gaming engine that tracks all the player's items?"
#15
Re: [C#] Create Array on summing some variables
Posted 25 April 2011 - 03:24 PM
and i can tell you, my goal is not to get the final code. i would like to understand how to do it in order to write my own code.
EDIT:
lol if it sounds like that you should read again my post. you will notice that this tool is for game masters. its a utility to create the items FOR the players, and not to get their items. ok? i'm working on that tool to simplify and speed up the "manual" work of the game masters and in the same time to learn C#!! why do you think i would do such mean things?!?!
This post has been edited by Anthonidas: 25 April 2011 - 03:31 PM
|
|

New Topic/Question
Reply



MultiQuote





|