# how to store this series in an integer array

Page 1 of 1

## 12 Replies - 938 Views - Last Post: 07 October 2012 - 07:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294581&amp;s=6f7b56fa49ce2a44d04863b76950d34b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lp94

Reputation: 0
• Posts: 7
• Joined: 06-October 12

# how to store this series in an integer array

Posted 06 October 2012 - 10:46 PM

```#include<stdio.h>
#include<conio.h>
int i,j;
void main()
{ clrscr();
for(i=1;i<=9;i++)
{
printf("%d",i);
if(i==1)
continue;
if(i%2!=0)
{printf("%d",i);
}
}
```

Objective :
i want to store the output of this series in an integer array ..
and print the sum
the average of the numbers
and sq. of sum of the numbers of the series by functions

problems:
if i declare an array and use a 2nd loop and use i as both the array's index and the 2nd loop's index then i can't get to store elements of the series in the array

i tried to use 2 seperate arrays but that didn't work for me either as i couldn't merge both of them into another array
so then i could use that third array in the functions .. and even then how would i use the array in the functions .. by another loop ?

Is This A Good Question/Topic? 0

## Replies To: how to store this series in an integer array

### #2 Skydiver

• Code herder

Reputation: 4367
• Posts: 14,079
• Joined: 05-May 12

## Re: how to store this series in an integer array

Posted 06 October 2012 - 10:52 PM

Pick an indent style and apply it consistently. Even considering how short your code is, it is still very hard to read and comprehend it.

### #3 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 02:28 AM

Also, I don't see how your code is even remotely relevant to your question.

### #4 lp94

Reputation: 0
• Posts: 7
• Joined: 06-October 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 02:44 AM

```#include<stdio.h>
#include<conio.h>
int a[13];
int b;
int a2[13];
int main()
{          clrscr();
for(b=1;b<=13;b++)
{
a[b]=b;
printf("%d",a[b]);
if(a[b]==1)
continue;
if(a[b]%2!=0)
{ a[b]=b;
printf("%d",a[b]);
}
if(b==9)
break;
}
//{if(a[b]<=2)
//{a2[b]=b;
//printf("%d",a2[b]);
// continue;}
//if(b>2 && a[b]<=9&&a[b]%2!=0&&a[1]==1)
//{
//a2[b]=b;
//printf("%d",a2[b]);
//printf("%d",a2[b]);

getch();
return 0;
}
```

here's my progress so far .. i've spent like a full day in trying to store the series in an array and i can't get it right..

### #5 lp94

Reputation: 0
• Posts: 7
• Joined: 06-October 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 02:50 AM

how do i use this series in functions to calculate the average and sum ? how do i work with the loop index and the array's index ..

### #6 Skydiver

• Code herder

Reputation: 4367
• Posts: 14,079
• Joined: 05-May 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 04:04 AM

I feel that you are going about storing into the array because you are fixated on using the for loop variable both as the value that is to be stored as well as an an array index. The trick is to use another variable.

This code will pick out the capital letters from a string and just store that into an array.
```char input[] = "The Quick Brown Fox Jumped Over The Lazy Dog.";
int inputLen = strlen(input);
char justCaps[256];
int numCaps = 0;

for(int i = 0; i < inputLen; ++i)
{
if (isupper(input[i]))
justCaps[numCaps++] = input[i];
}

```

### #7 Skydiver

• Code herder

Reputation: 4367
• Posts: 14,079
• Joined: 05-May 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 04:15 AM

Now as for you what your code needs to do, you don't even need the array. The following pseudo code computes the values you need:

```int sum = 0;
int count = 0;

// Account for line 8 of Post #1 series generator
for(int i = 1; i <= 9; i++)
{
sum += i;
count++;
}

// Account for lines 11-13 of Post #1 series generator
for(int i = 3; i <= 9; i += 2)
{
sum += i;
count++;
}

print sum
print sum / count as the average
print sum * sum as the square of the sum of the numbers in the series

```

This post has been edited by Skydiver: 07 October 2012 - 04:16 AM

### #8 Mohith Reddy

Reputation: -1
• Posts: 1
• Joined: 07-October 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 05:14 AM

```#include<stdio.h>
int i,j=0,sum=0,sumsq=0; //for sum and sum of squares
int a[100]={0};
void main()
{
for(i=1;i<=9;i++)
{

printf("%d",i);
a[j]=i;
j++;
if(i==1)
continue;
if(i%2!=0)
{
a[j]=i;
j++;
printf("%d",i);
}
}
printf("\n");
for(i=0;i<j;i++)
printf("%d",a[i]);
for(i=0;i<j;i++)
{
sum+=a[i];
sumsq+=(a[i]*a[i]);
}
printf("\n%d",sum);
printf("\n%d",sumsq);

}
```

MOD EDIT: Added code tags. When posting code, USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 07 October 2012 - 06:13 AM

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

## Re: how to store this series in an integer array

Posted 07 October 2012 - 06:14 AM

We don't just hand out code, even crappy code.

### #10 Skydiver

• Code herder

Reputation: 4367
• Posts: 14,079
• Joined: 05-May 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 06:44 AM

Additionally, that crappy code is computing the the sum of squares, but what the OP is looking for is the square of the sum.

### #11 lp94

Reputation: 0
• Posts: 7
• Joined: 06-October 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 07:45 AM

Thank you all for the codes
now i need guidance as to how to get the average of the numbers
and sq. of sum of the numbers of the series by functions
that was the Objective !! thanks again for the series SkyDiver and Mohit!!
i need some more help about the functions .. the code is getting too messy

### #12 jimblumberg

Reputation: 4734
• Posts: 14,757
• Joined: 25-December 09

## Re: how to store this series in an integer array

Posted 07 October 2012 - 07:48 AM

Post your current properly formatted code. And you may want to study the excellent function tutorials that are contained in my signature.

Jim

### #13 lp94

Reputation: 0
• Posts: 7
• Joined: 06-October 12

## Re: how to store this series in an integer array

Posted 07 October 2012 - 07:54 AM

im not gonna lie .. im stuck