Calculating ObjectAtIndex items in tableview - arrays

I have an array which contains my product prices.They are number type on my pList. I can reach them with this code.
[[mainList objectAtIndex:indexPath.row] objectForKey:#"Adi"];
I can show these items with my cell view. But I want to summarise the items and set my Total label.text
For Example :
[[mainList objectAtIndex:indexPath.row] objectForKey:#"Adi"] = 55 in my plist value.
another :
[[mainList objectAtIndex:indexPath.row] objectForKey:#"Adi"] = 45 in my pList value.
I want to summarise items like first one + second one = my total.
55+45 = 100 $
It should summarise my first row item + my second row item.
How can I calculate those items ?
Thank you very much !
Edited...
I tried these codes ;
for (int i=0; i<[mainList count]; i++) {
int a=[[[mainList objectAtIndex:0] objectForKey:#"dAdi"] integerValue];
int b=[[[mainList objectAtIndex:i] objectForKey:#"dAdi"] integerValue];
int final = a + b;
self.lblToplamTutar.text=[NSString stringWithFormat:#"%i",final];
it doesnt work correctly.
Ok. I found the solution.
There is the method for summarise array objects.
-(void)toplamaYap{
int sum=0;
for(int i=0; i < [mainList count]; i++)
{
sum += [[[mainList objectAtIndex:i]objectForKey:#"dAdi"] intValue];
}
self.lblToplamTutar.text = [NSString stringWithFormat:#"%d", sum];
}
Thank you for replies. Especially Santi Bernaldo...

If you are getting these values from your plist, I assume you are receiving NSNumber values, type of object for getting numbers values allowed with Plist serialization.
So sending the message "integerValue" to your NSNumber objects:
int i=[[[mainList objectAtIndex:0] objectForKey:#"Adi"] integerValue];
int j=[[[mainList objectAtIndex:1] objectForKey:#"Adi"] integerValue];
int final = i + j;
self.totalLabel.text=[NSString stringWithFormat:#"%i",final];

Related

Two errors I'm getting in this C program. Deals with structures, arrays, and bubble sorting

First two errors:
cs1713p3.c:129: error: incompatible types in assignment
cs1713p3.c:131: error: conversion to non-scalar type requested
Lines 129 and 131 are parts of a bubble sort function I wrote, which is meant to sort an array of a structure called "Stock". Here's the code for the Stock structure:
typedef struct
{
char szStockNumber[7]; // Stock Number for a stock item
long lStockQty; // quantity in stock
double dUnitPrice; // price per unit of stock
char szStockName[31]; // name of the stock item
} Stock;
Here's the code I wrote for the sort:
void sortInventory(Stock stockM[], int iStockCnt)
{
Stock *temp;
int i;
int j;
int bChange = 1;
for(i = 0; i < (iStockCnt - 1) && bChange == 1; i++)
{
bChange = 0;
for(j = 0; j < (iStockCnt - i - 1); j++)
{
if(strcmp(stockM[i+1].szStockNumber, stockM[i].szStockNumber) < 0)
{
temp = stockM[i]; //line 129
stockM[i] = stockM[i+1];
stockM[i+1] = (Stock)temp; //line 131
bChange = 1;
}
}
}
}
Yeah, I tried typecasting each assignment with a (Stock) right after the equals sign in each line, but that didn't work.
Thanks to anyone who helps out! I have no clue what's going wrong and I can't seem to find any relevant information on the internet, I think it's too specific of a case, haha.
stockM is an array of type Stock. temp, on the other hand, is an array of type Stock*. So, you are basically trying to assign the value of a variable of type Stock to a variable of type Stock* which is not allowed.
Change the type of temp to Stock.

How to take index, name and number as parameters?

Hi i was given a task in Processing and the question is this "You are asked to store a small telephone contact list that can hold ten names and ten matching telephone numbers. show how you would write a function that takes a name, a telephone number and index as parameters and sets the array(s) at the index position to the values given. Include in your answer code that checks the index given is a valid position in your array(s)."
I've come up with the code below
String[] names = new String[10];
int[] numbers = new int[10];
String[] contact = new String[10];
void setup() {
names[0] = "p1";
names[1] = "p2";
names[2] = "p3";
names[3] = "p4";
names[4] = "p5";
names[5] = "p6";
names[6] = "p7";
names[7] = "p8";
names[8] = "p9";
names[9] = "p10";
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
numbers[5] = 6;
numbers[6] = 7;
numbers[7] = 8;
numbers[8] = 9;
numbers[9] = 10;
for (int i=0; i<10; i++) {
contact[i] = "Name:"+names[i] +" "+ "Number:" + numbers[i]+" ";
println(contact[i]);
}
}
But my teacher's comment to my code were this "So what I expected here was a simple function that took an index, a number and a name as parameters and then filled in the arrays at the index value with the values." I'm quite confused about this question, is void setup not a function? and i really don't know what it means by making index, number and name as parameters. So if anyone can point out to me thanks!
EDIT: improved code
void setup() {
for (int i=0; i<10; i++){
contactList(i, "aName", 123456789);//default value to all 10 elememnts
}
}
void contactList(int index, String name, int number) {
println (index, "Name:" + name, "Number:" + number);
}
Do you know how to write a function? Hint: you do, you wrote the setup() function! Can you write another function that takes parameters? Start with a simple function that just takes a parameter and prints it out. Work from there.
Here's a little example that takes a String as a parameter and prints it out, which is called from the setup() function:
void setup(){
printMe("hello!");
}
void printMe(String text){
println(text);
}
Recommended reading:
Passing Information to a Method or a Constructor - Java Tutorials
Writing Your Own Functions - Static Void Games Tutorials

Items and Subitems in List-View control

I'm want to use a List-View control to display results of an LDAP search in a "grid". I've written some test code to see how it works, but it's not being displayed as I want. As I understand it, each Item is equivalent to a "row" (using LVS_REPORTstyle), and the Subitem is equivalent to a "column" (e.g. for each item I can display a number of subitems, each in a separate column on the same row).
Here's my test code, currently set to create four columns, with a single Item and four Subitems (corresponding to the four columns). Two functions: one to create the columns, the other to insert items.
int CreateColumns(HWND *hwndlistbox)
{
wchar_t *cnames[100];
LVCOLUMN lvc;
int i;
cnames[0] = L"column1";
cnames[1] = L"column2";
cnames[2] = L"column3";
cnames[3] = L"column4";
cnames[4] = NULL;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for (i = 0; cnames[i] != NULL; i++)
{
lvc.iSubItem = i;
lvc.pszText = cnames[i];
lvc.cx = 100;
lvc.fmt = LVCFMT_LEFT;
ListView_InsertColumn(*hwndlistbox, i, &lvc);
}
return i;
}
void InsertItems(HWND *hwndlistbox, int *columncount)
{
LVITEM lvi;
wchar_t *items[100];
int i, j;
items[0] = L"text1";
items[1] = L"text2";
items[2] = L"text3";
items[3] = L"text4";
items[4] = NULL;
lvi.mask = LVIF_TEXT;
lvi.iItem = 0;
for (i = 0; i < *columncount; i++)
{
lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_InsertItem(*hwndlistbox, &lvi);
}
}
I expect this to generate a single row (lvi.iItem = 0;) with a text string under each column (lvi.iSubItem = i;). This is what it displays instead:
Changing lvi.iSubItem = i to lvi.iSubItem = 0 results in each text string being displayed as a new row in the first column:
I've played around with it, hardcoding the numbers on both iItem and iSubItem, changing both to i, but I can't get it to display the text anywhere other than the first column. What am I doing wrong?
First of all, your cnames and items arrays are declared as array of pointers, but you are not allocating memory for them; you would need to declare them as an array of strings, like wchar_t cnames[100][40];.
Secondly, you need to use ListView_InsertItem to insert an item and set the value for the first column, then use ListView_SetItem to add additional columns, like
lvi.pszText = items[0];
lvi.iSubItem = 0;
ListView_InsertItem(*hwndlistbox, &lvi);
for (i = 1; i < *columncount; i++)
{ lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_SetItem(*hwndlistbox, &lvi);
}
Each row shows a single item so you cannot populate the columns by adding items.
As the documentation says:
"You cannot use ListView_InsertItem or LVM_INSERTITEM to insert subitems. The iSubItem member of the LVITEM structure must be zero. See LVM_SETITEM for information on setting subitems."
The LVM_SETITEM documentation explains how to set the text of a sub-item.

MQL4 array creating variables

I'm trying to make this function create X number of variables using an array. I know that this is technically wrong because I need a constant as my array's value (currently 'x'), but excluding that, what am I missing? Looked at so many code samples and can't figure it out, but I know it's got to be simple...
void variables()
{
int i;
int bars = 10;
int x = 1;
for (i = 1; i <= bars+1; i++)
{
int variables[bars] = { x };
x++;
if (i >= bars+1)
{
break;
}
}
void variables()
{
int bars = 10;
if(bars >= Bars) bars = Bars - 1;
// to be able to set array size based on variable,
// make a dynamically sized array
double highvalues[];
ArrayResize(highvalues, bars);
for (int i = 0 /*Note: Array index is zero-based, 0 is first*/; i <= bars; i++)
{
highvalues[i] = iHigh(NULL, 0, i);
// or
highvalues[i] = High[i];
}
}
It is hard to tell what do you want to achieve.
If you want to fill an array with a value ArrayFill() fill help you.

Cartesian Product of multiple array

I think it is basically an easy problem, but I'm stuck. My brain is blocked by this problem, so I hope you can help me.
I have 2 to N arrays of integers, like
{1,2,3,4,5}
{1,2,3,4,5,6}
{1,3,5}
.....
Now i want to have a list containing arrays of int[N] with every posibillity like
{1,1,1}
{1,1,3}
{1,1,5}
{1,2,1}
....
{1,3,1}
....
{2,1,1}
{2,1,3}
....
{5,6,5}
so there are 6*5*3 (90) elements in it.
Is there a simple algorithm to do it? I think the language didn't matter but I prefer Java.
Thx for the help!
I add a valid answer with the implementation in java for the next guy, who has the same problem. I also do it generic so u can have any CartesianProduct on any Object, not just ints:
public class Product {
#SuppressWarnings("unchecked")
public static <T> List<T[]> getCartesianProduct(T[]... objects){
List<T[]> ret = null;
if (objects != null){
//saves length from first dimension. its the size of T[] of the returned list
int len = objects.length;
//saves all lengthes from second dimension
int[] lenghtes = new int[len];
// arrayIndex
int array = 0;
// saves the sum of returned T[]'s
int lenSum = 1;
for (T[] t: objects){
lenSum *= t.length;
lenghtes[array++] = t.length;
}
//initalize the List with the correct lenght to avoid internal array-copies
ret = new ArrayList<T[]>(lenSum);
//reusable class for instatiation of T[]
Class<T> clazz = (Class<T>) objects[0][0].getClass();
T[] tArray;
//values stores arrayIndexes to get correct values from objects
int[] values = new int[len];
for (int i = 0; i < lenSum; i++){
tArray = (T[])Array.newInstance(clazz, len);
for (int j = 0; j < len; j++){
tArray[j] = objects[j][values[j]];
}
ret.add(tArray);
//value counting:
//increment first value
values[0]++;
for (int v = 0; v < len; v++){
//check if values[v] doesn't exceed array length
if (values[v] == lenghtes[v]){
//set it to null and increment the next one, if not the last
values[v] = 0;
if (v+1 < len){
values[v+1]++;
}
}
}
}
}
return ret;
}
}
As i understand what you want, you need to get all permutations.
Use recursive algorithm, detailed here.
As I see this should work fine:
concatMap (λa -> concatMap (λb -> concatMap (λc -> (a,b,c)) L3) L2) L1
where concatMap(called SelectMany in C#) is defined as
concatMap f l = concat (map f l).
and map maps a function over a list
and concat(sometimes called flatten) takes a List of List and turns it into a flat List

Resources