12 Replies - 623 Views - Last Post: 07 October 2012 - 07:54 AM Rate Topic: -----

#1 lp94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 lp94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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..
Was This Post Helpful? 0
  • +
  • -

#5 lp94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ..
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • 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];
}


Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 Mohith Reddy  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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!!!

:code:

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

Was This Post Helpful? -1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 lp94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :(
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,897
  • 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
Was This Post Helpful? 0
  • +
  • -

#13 lp94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

yes ok i am reading through your and Jack of all trades Functions tutorials
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1