adding variables for, for loop


 
Thread Tools Search this Thread
Top Forums Programming adding variables for, for loop
# 1  
Old 03-10-2006
adding variables for, for loop

I have a structure which contains n number of elements. For example:
stFruits : apple, grapes, strawberry, pear, kiwi, melon, papaya, mango, orange, sweetlime ..... etc

Now i have to write a for loop as follows:

int i;
int j;

j=stFruits.apple+stFruits.grapes+stFruits.pear+.... and so on...

for(i=0;i<j;i++)
{
}

What i would like to know is , is there an other way to represent the sum of all elements in the structure instead of adding in the above way???

Iam very new to Programming... and hope to get an answer...
# 2  
Old 03-10-2006
Not really, you could avoid using the j and check for
Code:
for(i=0;i<stFruits.apple+...;i++) {
}

I don't think that there is any other way to get the sum of structure elements. Maybe you could declare them in an array and use a loop to get the sum? If not, then you have no choice but to go about it this way.
# 3  
Old 03-13-2006
Quote:
What i would like to know is , is there an other way to represent the sum of all elements in the structure instead of adding in the above way???
one more way;
but an extra element to be populated in the node.

your prev definition of the structure.
Code:
struct stFruits
{
  int fruittype;
  struct stFruits *next;
};

modifiy the structure definition as
Code:
struct stFruits
{
  int fruittype;
  long cumSum;
  struct stFruits *next;
};

when adding each of the element to the list of nodes,
just populate the cumSum
and to retrieve the totalSum just read the lastelement.cumSum,

that would do.
# 4  
Old 03-13-2006
Thank u so much for the reply.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Adding same value to variables in does each repetition of command

So, I have this command: mkdir rolled for %%x in (*gif) do convert %%x -roll +2+6 %%x|move %%x rolled I'd like to have the +2 and +6 accumulate here. In each new gif tackled, it should increase by the amount: +2 (for x) and +6 (for y) Is this possible? I'm on Windows, DOS. (0 Replies)
Discussion started by: pasc
0 Replies

2. UNIX for Dummies Questions & Answers

Adding variables to repeating strings

Hello, I want to add a letter to the end of a string if it repeats in a column. so if I have a file like this: DOG001 DOG0023 DOG004 DOG001 DOG0023 DOG001 the output should look like this: DOG001-a DOG0023-a DOG004 DOG001-b (15 Replies)
Discussion started by: verse123
15 Replies

3. Shell Programming and Scripting

adding one more column in a loop

data1 1 0.01 3 5 1 0.6 2 1 data2 2 0.02 3 5 2 0.3 2 1 data3 3 0.01 3 5 3 0.01 2 1 output 1 0.01 data1 2 0.02 data2 3 0.01 data3 3 0.01 data3 I want to print 1st, 2nd column and the filename when second column is less than 5. (3 Replies)
Discussion started by: johnkim0806
3 Replies

4. Shell Programming and Scripting

Adding a while loop to my script

I have a script below that goes to the given directory and plays the newest powerpoint presentation via powerpoint viewer and wine. So far it works perfectly but now Id like to add a while statement to essentially run find /ticker/powerpointshare -mmin -1 -type f -iname "*.ppt" and if it finds a... (9 Replies)
Discussion started by: binary-ninja
9 Replies

5. Shell Programming and Scripting

Adding Variables

Hi. I have a for loop that I use to extract integer values in a shell script (ksh). Now, I would like to add the values. My preference, from my c programming days, would be to do something like the commented out line below in the for loop. However, this is not recognised. So I use the line... (2 Replies)
Discussion started by: mikem22
2 Replies

6. Solaris

Creating script adding 3 different variables in 3 columns

I have 3 variables with different information.. they look like this (row-wise aswell): Variable1 = Roland Kalle Dalius Variable2 = ake123 ler321 kaf434 Variable3 = Richardsen Sworden Lokthar How can I sort them by variable3 alphabetical and add them into the same output so... (0 Replies)
Discussion started by: Prantare
0 Replies

7. Shell Programming and Scripting

doing a for loop adding up the results

Hi there If I run a 'swap -l' on my solaris box, i get swapfile dev swaplo blocks free /dev/dsk/c1t0d0s1 54,65 8 67119560 65655144 /dev/dsk/c1t0d0s2 54,65 8 33119522 32655122 I wanted to run a for loop adding up the totals of each column 4 , excluding the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Shell Programming and Scripting

Adding variables in a unix script

Hi I am trying to add variables(float values) in a unix script but am getting an error value=`expr $a + $b + $c` The error I am getting is "expr: non-numeric argument" I guess it has got to something with the decimal points. Plz help (13 Replies)
Discussion started by: akashtcs
13 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies
Login or Register to Ask a Question