Most of the time in your loop you need to print one value and one comma. That's easy. What may not be obvious is that you can print the comma either before the number or after the number. You are doing it the former way (which is the obvious way -- usually a reasonable place to start) and so you need to find a way to detect that you are printing out the last value and not print a comma in that case. Can you see how to rewrite your printf() statement so that it prints the comma before the number so that, except for the beginning and end of the line that is printed out, it looks the same as what you have now. If so, then your problem has been changed to detecting that you are printing the first value and not printing a comma in that case. Which is easier to detect, that you are printing the first value or the last value?

But you are losing site of the big picture -- we are exploring using arrays to efficiently solve problems and we are working with "toy" problems to keep things simple. So while we might be working with five values in our little programs here, we are trying to develop skills and techniques that allow us to write programs that solve similar problems but that are MUCH larger in scale.

Even with this toy problem, try to apply your approach here to your prior program in which you asked the user to enter how many data elements were going to get stored in the array. Since you don't know how many data elements they are going to work with, you would have to have a huge set of if() statements to trap each one. And then what if the size of the problem could have them entering thousands of data values.

1) Write a program that asks the user for five integer values.
2) Print them out as a comma-separated list of values on one line.
3) Print out the sum of the five values on the next line.
4) Ask the user which value they want to double, with 1 being the first value entered and 5 being the last value entered.
5) Print out the five values again.
6) Print out the sum of the five values on the next line.
.

You also aren't doing what the problem statement requested. Are you trying to, or just doing something different? Either is fine, but I need to know which in order to provide useful feedback.

Assuming you are trying solve the problem as stated, notice that the user is only entering a single data set. The problem then prints out that data set and it's sum. Then it asks the user which value within the original data set should get doubled (with the first value being value #1) and prints out the modified data set and the sum of the modified data set.

You also aren't printing out the commas between the values (and only between them).

One thing you want to get in the habit of doing is formatting your code cleanly and consistently. You aren't doing too bad (much better than most) and formatting code when posting to the forum is admittedly a pain because (in most browsers) you have to enter the code in a proportional-spaced font but it will be displaced monospace (and, unfortunately, if you copy/paste from another source multiple spaces are reduced to a single space -- very annoying) so you just have to count spaces as you type.

One thing that can make your code more readable is to not use enclosing braces for single line blocks of code. But there is a downside in that if you later add a second line you may forget to add the now-mandatory braces. There are different schools of thought on this one.

Cleaning up the formatting let me see a problem that I didn't see before.

It's now obvious that the

printf ( "value : ", array ); /*Print list of values on one line */

statement is not included in the for() loop above it. This made me look a bit closer and I can see that this statement can't produce the output you show -- you've tweaked the code someone between running it and pasting it into your post. The printf() statement above lacks a format specifier, so it will not even look at the second argument. Which is just as well because the second argument, if size = 5, is array[5] (since that is the value of size with the for() loop exits), which has not been set by the user.

Now let's clean up the code a bit -- without changing the logic any.

You use a different variable in each of your for() loops. This is generally not recommended unless it is necessary to do so, for the symbol reason that people reading your code will be led to believe that it is necessary to do so when they see you do this and waste a bunch of time figuring out that your code doesn't actually need to do this. So when you have a dummy variable used as a loop counter, try to use the same one. An even better way is to make the variable local to the loop, but we can discuss that at some later time.

Another style problem is with your initialization of sum. You do it in two places and neither is a good place. The first one is unnecessarily far from where it is needed and the second is needed for the next iteration of the loop by code that precedes it. Try to see if there is a single place that you can initialize loop that is close to where it is used. This makes the code more modular.

Another common style (but there are others) is to put a space between control structures and opening parens but to not do so between a function name and the opening parens. What matters more is being consistent. You code was pretty consistent -- just a single place where you didn't have a space in both cases.

One thing that can make your code more readable is to not use enclosing braces for single line blocks of code. But there is a downside in that if you later add a second line you may forget to add the now-mandatory braces. .

Click to expand...

I tried to follow that style but I was getting so many errors so I used enclosing braces

What in the problem specifications says anything about making multiple passes?

The first part of solving a problem is making sure you understand the problem -- it is also often the hardest part of problem solving, particularly in the real world.

So let's review the problems specifications very carefully:

1) Write a program that asks the user for five integer values.
2) Print them out as a comma-separated list of values on one line.
3) Print out the sum of the five values on the next line.
4) Ask the user which value they want to double, with 1 being the first value entered and 5 being the last value entered.
5) Print out the five values again.
6) Print out the sum of the five values on the next line.

This problem is very narrow in it's focus. Don't go expanding it until you can solve this narrow version of it.

The point of the problem is to practice using arrays to solve problems.

In step #1 you are told to ask the user for five integer values. You are not told to ask the user for how many values they want to enter. You are told to ask them for five values. Nothing more, nothing less.
In step #2 you are told to print out those five values as a comma-separated list on one line.
In step #3 you are told to print out the sum of those five values on the next line.
In step #4 you are told to ask the user which of the original five values they want to double, with a 1 being the first value and 5 being the last value.
In step #5 you are told to print the list of values again. This should also be as a comma-separated list on one line and should be identical to the original list except that one of the values should be twice as large.
In step #6 you are told to print out the sum of the five values on the next line. This number should be equal to the original sum printed out earlier plus the original value of the number they chose to double.

Other than error checking (and graceful death is perfectly acceptable at this stage), your program should do NOTHING beyond what the problem specifies. In fact, it is perfectly reasonable to initially solve the problem assuming that nothing will go wrong and add the error checking later (though another school of thought says to do the input validation first before writing any code that actually solves the problem -- both approaches have pros and cons).

You've got many of the pieces in place, but you keep going off on a tangent and ignoring the details of the problem. Focus on the specifics of the problem.

Given n values stored in an int array, print them out as a single-line comma-separated list.

So let's flesh out exactly what has to happen to make a single-line comma-separated list of values.

#1) Print out the first value.
#2) FOR each remaining value.
#2a) Print a comma followed by a space.
#2b) Print the value.
#3) Print the end of line (newline character).

Let's assume that we have int variables i and n declared where n is equal to the number of values stored in the array. We also have an int array names data that is appropriately dimensioned and already has the data stored in it.

Let's do exactly what our list above says

Code (Text):

printf("%d" data[0])

for(i = 1; i < n; i++)

{

printf(", ");

printf("%d", data[i]);

}

printf("\n");

Do you see what I did? I broke the problem into a set of small tasks such that each task could be translated easily into a code fragment that performs that small task.

Is this the only way to solve this problem? No. There are many ways. Let's look at a couple.

#1) FOR each value.
#1a) Print the value.
#1b) IF the value is not the last value.
#1b1) Print a comma followed by a space.
#2) Print the end of line (newline character).

The code that this translates to is:

Code (Text):

for(i = 0; i < n; i++)

{

printf("%d", data[i]);

if ( i != n - 1 )

printf(", ");

}

printf("\n");

Here's another variant:

#1) FOR each value.
#1a) IF the value is not the first value
#1a1) Print a comma followed by a space.
#1b) Print the value.
#2) Print the end of line (newline character).

Code (Text):

for(i = 0; i < n; i++)

{

if ( i != 0 )

printf(", ");

printf("%d", data[i]);

}

printf("\n");

Yet another variant that represents a little bit more refined approach:

#1) FOR each value.
#1a) Print the value.
#1b) IF the value is the last value.
#1b1) Print the end of line (newline character).
#1c) ELSE
#1c1) Print a comma followed by a space.

Which becomes:

Code (Text):

for(i = 0; i < n; i++)

{

printf("%d", data[i]);

if ( i == n - 1 )

printf("\n");

else

printf(", ");

}

As a teaser of things you can do (so don't be concerned if this looks confusing right now -- you'll get to it at some point), you can tighten this up quite a bit as follows:

Code (Text):

for(i = 0; i < n; i++)

printf("%d%s", data[i], (i == n-1)? "\n" : ", ", data[i]);

This used the "conditional operator", also known as the "ternary" operator. When you get to that point, come back and see if you can understand what this is doing.

Oh -- and note that I haven't compiled and run these snippets, so it's possible that a mistake snuck and that I didn't catch. I'm more than happy to correct anything that gets spotted.

I tried to follow that style but I was getting so many errors so I used enclosing braces

Click to expand...

That's fine. It's a valid approach, but you do need to be able to read and write code that doesn't enclose single statements, both because you will see a lot of code that uses that style and because, if you can't, that means that you really don't know program structure and syntax very well. But it isn't something that has to happen overnight.

If the array size is not valid then ask user until he doesn’t enter correct array size

if the array size is valid then store number into array

Print out the sum of the array values on the next line

Ask the user which value they want to double, with 0 being the first value entered and size being the last value entered.

Print the double value

Repeat process as user want -pass value

If you want the program as you said I will do it but, Is it not complete requirement you said?

if you type input as you said it will give output you wanted. I am sure because I have tested it many time on my computer GCC tool

Click to expand...

If you want to solve a different problem, fine. But notice how you posted the program requirements as I stated them, then claimed that you had written a program that satisfied those requirements, the posted code that solved some other unstated set of requirements and NOT the program requirements as posted. That's a recipe for nothing but confusion.

Also note that the requirements listed above do not match what your program does. Your program prints out the values on a single line with only a space between them. Which of the requirements above involves doing that?

You also have created additional problems similar to ones you had before. You ask the user for an array size with a limit of 0 to 32. But you dimension your array for 20. If the user enters 30, where does the rest of the data go?

Run your program and enter 0 as the array size. Does it do what you expect? Now run it and enter 32 as the array size. Does it do what you expect?

1) Write a program that asks the user for five integer values.
2) Print them out as a comma-separated list of values on one line.
3) Print out the sum of the five values on the next line.
4) Ask the user which value they want to double, with 1 being the first value entered and 5 being the last value entered.
5) Print out the five values again.
6) Print out the sum of the five values on the next line.

This problem is very narrow in it's focus. Don't go expanding it until you can solve this narrow version of it.

The point of the problem is to practice using arrays to solve problems.
In step #5 you are told to print the list of values again. This should also be as a comma-separated list on one line and should be identical to the original list except that one of the values should be twice as large.
In step #6 you are told to print out the sum of the five values on the next line. This number should be equal to the original sum printed out earlier plus the original value of the number they chose to double.
.

Click to expand...

I am stuck in step five. How to replace double value with original value

I am stuck with array I don't understand how to replace only one value that has been double

Click to expand...

You are not doing what was specified.

"you do what is needed to make it so that the value stored in x becomes 84 and then you print out the value stored in x."

You are not changing the value stored in x. It is remaining whatever it was that the user entered. You are not printing out the value stored in x (in the second printf() statement, you are printing out the value stored in x multiplied by 2.

Notice that in both lines of the specification, the print portion was identical.

If asked to enter a value for x and the user enters 42, then the value stored in x should be 42 and you then print out the value stored in x.

If asked to double it, you do what is needed to make it so that the value stored in x becomes 84 and then you print out the value stored in x.

So whatever code prints out the value stored in x the first time should be identical to the code that prints out the value stored in x the second time.