?
Solved

loop program

Posted on 2007-10-11
3
Medium Priority
?
229 Views
Last Modified: 2010-04-15
how to do this program
thanks guys

Ï initialize an integer 'i' to 23
Ï print a new line with the value of 'i'
Ï run a for loop with the following requirements
Ë use a local integer variable named 'i' as the loop variable
Ë initialize the for loop to 0
Ë the for loop must loop 10 times
Ë post increment the loop variable
Ë print a new line with the value of the loop variable inside the for loop code block
Ï after the for loop, print a new line with the value of 'i'
0
Comment
Question by:tsburt
3 Comments
 
LVL 1

Author Comment

by:tsburt
ID: 20063012
this is what i ahve so far..

#include <stdio.h>

int main(void)
{
int x;
for x=0; x<=23; x++) printf("%d ", x);
0
 
LVL 24

Accepted Solution

by:
fridom earned 1400 total points
ID: 20063309
Well you are nearly done
int main (void) {
   int i = 23;
   printf("%d\n", i); /* the \n prints the new line */
   /* then in the loop do just wha you were asked to do:
   for (i = 0;  i < 10; i++) {
     /* output */

i++ the postincrement

Now in C99 this probably is allowed also
    int i = 23

    for(int i = 0;

The for may open a new scope, but I doubt that was meant it seems you want to reuse the
local variable i.

Regards
Friedrich
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 600 total points
ID: 20063437
>> Ï initialize an integer 'i' to 23

As fridom already pointed out : you used x instead of i. And you didn't initialize it yet.


>> Ï print a new line with the value of 'i'

Take a look at the printf standard function (in <stdio.h>)


>> Ï run a for loop with the following requirements

Your for loop is a good start, but you're missing a ( somewhere.

>> Ë use a local integer variable named 'i' as the loop variable

Same comment as earlier : i instead of x

>> Ë initialize the for loop to 0

That's ok.

>> Ë the for loop must loop 10 times

This still has to be fixed. Also consider using < instead of <=

>> Ë post increment the loop variable

That's ok too.

>> Ë print a new line with the value of the loop variable inside the for loop code block

That's almost ok - you need to add a new line. You can use the newline character \n for that. Take a look at this reference page for printf eg. :

        http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

(especially at the example on that page)


>> Ï after the for loop, print a new line with the value of 'i'

You'll have to add one more printf here ...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question