Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 27th, 2013, 10:13 AM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0
Help for homework

Hi guys, hope you are well

I have homework

I tried to solve it but I couldn't

anyone can plz?

Write a program that sums a sequence of integers. Assume that
the first integer read with scanf specifies the number of values remaining to be entered. Your program
should read only one value each time scanf is executed. A typical input sequence might be
5 100 200 300 400 500
where the 5 indicates that the subsequent five values are to be summed.

#2
March 27th, 2013, 11:00 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,891
Time spent in forums: 3 Months 1 Day 19 h 27 m 46 sec
Reputation Power: 2199
So show us what you tried.

#3
March 27th, 2013, 11:08 AM
 eramit2010
Contributing User

Join Date: Mar 2013
Location: India
Posts: 95
Time spent in forums: 2 Days 18 h 10 m
Reputation Power: 3
its easy program but u should at least try something.
and if u tried anything then show it.

#4
March 27th, 2013, 11:49 AM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0
int arr[a];
int s,d;

printf("Enter the number");
scanf("%d", a);

for(s=0; s<a; s++)

{

printf("Enter number%d:", s);
scanf("%d", arr[a]);

}

#5
March 27th, 2013, 11:55 AM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0
Quote:
 Originally Posted by Rsh_rsh int arr[a]; int s,d; printf("Enter the number"); scanf("%d", a); for(s=0; s

i'm beginner in this language

#6
March 27th, 2013, 12:32 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,891
Time spent in forums: 3 Months 1 Day 19 h 27 m 46 sec
Reputation Power: 2199
We were all beginners at one time.

Start by writing down in English how you would do that by hand. Then expand that, again in English, with a step-by-step procedure. Then once you know what you're trying to do, you translate each English step into C.

#7
March 27th, 2013, 12:43 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,891
Time spent in forums: 3 Months 1 Day 19 h 27 m 46 sec
Reputation Power: 2199
Quote:
 Originally Posted by Rsh_rsh int arr[a]; int s,d; printf("Enter the number"); scanf("%d", a); for(s=0; s

Use code tags for posting code. Otherwise the code's indentation, which is absolutely needed to keep it readable, will be lost.

[code] insert your formatted code listing here [/code]
Eg:
Code:
```int arr[a];
int s,d;

printf("Enter the number");
scanf("%d", a);

for(s=0; s<a; s++)
{
printf("Enter number%d:", s);
scanf("%d", arr[a]);

}```

The variable a needs to be declared.

You cannot use a in the declaration for arr. There is a common way to create an array after you are told how big to make it, but you are obviously not at that point yet. Decide on some maximum size for arr and use that to declare it. Of course, if you are not expected to display those array value later, then you would have no need for an array.

scanf needs variable addresses, not values! Use the address operator (&) as your book has told you to do.

#8
March 27th, 2013, 01:06 PM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0
Quote:
 Originally Posted by dwise1_aol We were all beginners at one time. Start by writing down in English how you would do that by hand. Then expand that, again in English, with a step-by-step procedure. Then once you know what you're trying to do, you translate each English step into C.

thank you, i'll do it now

#9
March 27th, 2013, 01:17 PM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0
Quote:
 Originally Posted by dwise1_aol Use code tags for posting code. Otherwise the code's indentation, which is absolutely needed to keep it readable, will be lost. [code] insert your formatted code listing here [/code] Eg: Code: ```int arr[a]; int s,d; printf("Enter the number"); scanf("%d", a); for(s=0; s

first of all thanks for valuable information

and I thought I need array

anyway

im gonna read a lot, I'll try to make it

I started to learn c one week a ago

i'll try to next time to say I made it hopeful

#10
March 27th, 2013, 01:54 PM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0
Quote:
 Originally Posted by dwise1_aol We were all beginners at one time. Start by writing down in English how you would do that by hand. Then expand that, again in English, with a step-by-step procedure. Then once you know what you're trying to do, you translate each English step into C.

I need from the program to ask me "how many integers do you want"

Code:
```int a;

printf("how many integers do you want\n");

scanf("%d", &a);

```

then I can choose any number
lets say 3
then after that
the program ask me "enter first number"
then I entered 10
"enter second number"
I entered 100
"enter third number"
I entered 1000

Code:
```
for(b=0; b<; s++)

{

printf("Enter number %d\n", b);

scanf("%d", &b);

}```

then the program add them together

and show the result

it'll be 1110

what do you think

this is what I can do

#11
March 27th, 2013, 03:57 PM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0
Hi guys

after many hours

I got this

Code:
```
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

int arr[24],b,s;

printf("How many integers do you want\n\n");
scanf("%d", &s);

for(b=1; b<=s; b++)

{

printf("Enter Number%d:", b);
scanf("%d", &arr[b]);

}

printf("******************- LIST-****************\n");

for(b=1; b<=s; b++)

{

// printf("arr%d: %d\n", b, arr[b]);

printf("Number%d\n+\n",b );

}

system("PAUSE");
return 0;
}

```

but I wanna add them to get the result

can you help me for that

cause I tried but I couldn't

#12
March 27th, 2013, 11:25 PM
 winniethedev
Registered User

Join Date: Mar 2013
Posts: 1
Time spent in forums: 10 m 40 sec
Reputation Power: 0
Hello,

First, you need to declare & define four integers: number of integers you will be entering, a counter for the for loop, num to store the entered number and the sum.

Here is the actual code:

<edited - please don't help with complete code. Hints are ok>

Last edited by Scorpions4ever : March 28th, 2013 at 02:27 AM.

#13
March 28th, 2013, 07:14 AM
 eramit2010
Contributing User

Join Date: Mar 2013
Location: India
Posts: 95
Time spent in forums: 2 Days 18 h 10 m
Reputation Power: 3
Quote:
 Originally Posted by Rsh_rsh Hi guys after many hours I got this Code: ``` #include #include int main(int argc, char *argv[]) { int arr[24],b,s; printf("How many integers do you want\n\n"); scanf("%d", &s); for(b=1; b<=s; b++) { printf("Enter Number%d:", b); scanf("%d", &arr[b]); } printf("******************- LIST-****************\n"); for(b=1; b<=s; b++) { // printf("arr%d: %d\n", b, arr[b]); printf("Number%d\n+\n",b ); } system("PAUSE"); return 0; } ``` but I wanna add them to get the result can you help me for that cause I tried but I couldn't thanks in advance

take a sum variable and in 1st loop after scanning add scanned data in the sum variable.

#14
March 28th, 2013, 11:46 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,891
Time spent in forums: 3 Months 1 Day 19 h 27 m 46 sec
Reputation Power: 2199
Quote:
 Originally Posted by Rsh_rsh but I wanna add them to get the result can you help me for that cause I tried but I couldn't

Adding is one of the first things we learned in arithmetic. In C it's
c = a + b;
You can't get much more basic than that.

Since you're storing the input values in an array, use a for-loop to iterate through the array, adding each element to a sum variable that you declare. When you are done, that sum variable will contain the sum, which is what you are trying to calculate.

Remember this very basic algebraic identity:
a + 0 = a
which means that any value added to zero is itself. So when you declare that sum variable, initialize it to zero. Then when you've added all the elements of the array to sum, it will contain the sum of the elements of the array. Very simple.

Even simpler would be to simply add each number to sum when you input it. That would eliminate any need for an array. Unless you absolutely need to keep each input value for later (eg, to display them in a final report, to use them in another calculation).

Please review your textbook's chapter on arrays, paying especial attention to indexing. In C, an array's first element is indexed by zero, not by one. When you declare an array with N elements, you index them with zero through N-1. In your case, your array arr, having a size of 24, consists of the elements arr[0] through arr[23].

You are starting your array operations with 1, not with 0 as you should be doing, and thus ignore the first element. You can get away with that in this program, but only if you input fewer than 24 values. Your for-loops should read like this:
for(b=0; b<s; b++)
which will iterate s times.

Last edited by dwise1_aol : March 28th, 2013 at 11:49 AM.

#15
March 28th, 2013, 12:20 PM
 Rsh_rsh
Registered User

Join Date: Mar 2013
Posts: 8
Time spent in forums: 1 h 38 m 59 sec
Reputation Power: 0

Code:
```
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

int arr[0];
int a=0,b,s,sum=0;

printf("How many integers do you want\n\n");
scanf("%d", &s);

for(b=1; b<=s; b++)

{

printf("Enter Number%d:", b);
scanf("%d", &arr[b]);
sum = arr[b] + sum;

}

printf("******************- LIST-****************\n\n");

for(b=1; b<=s; b++)

{

printf("Number%d\n+\n",b );
}

printf("The total is %d\n", sum);

system("PAUSE");
return 0;
}

```

I would like to thank

1- dwise1_aol

2- eramit2010

3- winniethedev

I know that it was easy program

but really one day i'll be really good in c

be cause you guys

thank you very much

an have nice and amazing weekend

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Help for homework