•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Programming homework help

c nested loop

16 replies to this topic

CC Lurker

• Just Joined
• 6 posts

Posted 19 September 2011 - 09:59 AM

Hi,

because of some exams I have not started with my programming course and books. And in 32 hours i have an exam, and in 34 hours a project in programming is to be submitted to my teacher. PLEASE help me with this project. Would be eternaly greatfull!

Unsure if this is the right place to post thread though.
Project is as following:

Problem set:

1. In C, what is the difference between 3, '3', and "3"?
To solve this problem you need to read up on constants and strings in C.

2. Under what conditions will this code print "foo"?
```if (i < 10) {
printf("foo");
} else {
printf("bar");
}```

To solve this problem you need to read up on if statements in C.

3 Assuming i is a 32-bit integer with the value 0xffffffff, will the following code print "foo" or "bar"? Explain your answer.
```if (i > 10) {
printf("foo");
} else {
printf("bar");
}```

To solve this problem you need to read up on 2's complement representation.

4. What do the following loops print?
```for (i = 0; i < 5; i = i+1) {
printf("%d\n", i);
}
for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
for (i = 5; i >= 0; i = i-1) {
printf("%d\n", i);
}
for (i = 0; i <= 5; i *= 2) {
printf("%d\n", i);
}```

To solve this problem you need to read up on for loops in C.

5. Express these loops by use of while instead of for.
```for (i = 0; i < 5; i = i+1) {
printf("%d\n", i);
}
for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
for (i = 5; i >= 0; i = i-1) {
printf("%d\n", i);
}
for (i = 0; i <= 5; i *= 2) {
printf("%d\n", i);
}```

To solve this problem you need to read up on while loops in C.

6. Write a C function that takes an integer as parameter and returns that integer multiplied by 5. For example, if the function is called with the argument 5, it should return 25.
To solve this problem you need to read up on use of functions C.

7. Write a C function that prints the numbers from 1 to 50, along with an indication of whether the number is odd or even and whether 5 is a prime factor in the number. The function should print something like:
1 is odd and 5 is not a prime factor
2 is even and 5 is not a prime factor
..
10 is even and 5 is a prime factor

The C function should have the following signature:

void mynumbers(void)

8. To solve this problem you need to read up on use of the modulus operator in C.

Write a C function that prints a triangle. The number of lines in the triangle should be a parameter to the function. For example, if the function is called with the value 6 as an argument, the following triangle should be printed:

*
**
***
****
*****
******

The C function should have the following signature:

`void mytriangles(int numlines)`

To solve this problem you need to read up on how to express loops and how to create nested loops in C.

9. The binary logarithm of n (log2n) is the power to which the number 2 to must be raised to obtain the value n. Write a C function that calculates log2 of an unsigned integer n using the following approach: find the most significant set bit in n and return the position of this bit. For example, if n is 17 (0b10001), the function should return 4.

The C function should have the following signature:

`int mylog2(unsigned int n)`

To solve this problem you need to read up on how to express logical and bitwise shift operations in C.

Edited by Roger, 20 September 2012 - 11:54 AM.

• 0

### #2 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 19 September 2011 - 11:57 AM

• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #3 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 19 September 2011 - 12:16 PM

As I understand your question, you want us to do your work for you because you procrastinated. That's not likely going to happen. This is a forum for helping people with their questions. If you'll make a sincere effort, we would be glad to help you.
• 1

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
â€“ Douglas Hofstadter, GÃ¶del, Escher, Bach: An Eternal Golden Braid

CC Lurker

• Just Joined
• 6 posts

Posted 20 September 2011 - 06:39 AM

My answers so far is this.

I know most, if not all answers here is wrong. But I hope to get an input of what i have done wrong, and maybe explain why my answer is wrong.

Problem 1
3 is the decimal number, â€˜3â€™ Refers to the ASCII value of 3, and

Problem 2
if (i < 10) {
printf("foo");
} else {
printf("bar");
}

Will print foo if i is lesser than 10. And bar if larger than 10

Problem 3
if (i > 10) {
printf("foo");
} else {
printf("bar");
}
// The following code will give the print bar, as 0xffffffff is the number 4G in the hexadecimal system.

Problem 4
for (i = 0; i < 5; i = i+1) {
printf("%d\n", i);
}
Will Print 1,2,3,4

for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
Will Print 1,2,3,4

for (i = 5; i >= 0; i = i-1) {
printf("%d\n", i);
}
Will print 5,4,3,2,1,0

for (i = 0; i <= 5; i *= 2) {
printf("%d\n", i);
}
Will Print
• 0

### #5 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 September 2011 - 07:45 AM

2) correct
3) right answer, wrong reason. i is negative because the sign bit is set.
4) Copy the code, compile, and run it. Pretty easy to test your answers.
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #6 Flying Dutchman

Flying Dutchman

• Expert Member
• 1090 posts
• Location:::1
• Programming Language:C++, Python

Posted 20 September 2011 - 03:23 PM

2. What if i is 10, what will print then?
• 0

The roots of education are bitter, but the fruit is sweet.

CC Lurker

• Just Joined
• 6 posts

Posted 22 September 2011 - 09:13 AM

Problem 6

here is the code so far. I get the program to ask for my number, but I do not get it to myltiply my number with 5 and give me the answer.
How do i finish this code? :/

PS. Might seem like a complete noob, and that is because I am, at school i attend geology, and got programming just to get enogh points.
So be nice, dont bully me:P

int i;
int t=5

printf("Enter a number:");
scanf("%d", &i);

}
• 0

### #8 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 22 September 2011 - 09:32 AM

• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

CC Lurker

• Just Joined
• 6 posts

Posted 22 September 2011 - 10:27 AM

Thanks,

I see now that programming is pretty logical, I just have to learn the language.

But still some problems with problem 7.

int x = 50;
int i = 0;

while(i < x){
i++;
printf("%d\n",i);}

if(i .....)

now I want to say If i divided by 2 becomes a whole number (without a comma) the number is even
else
the number is odd.

but I can not see any way to specify that the answer shall be a number without comma.
• 0

### #10 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 22 September 2011 - 10:40 AM

if (i % 2 == 0)
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

CC Lurker

• Just Joined
• 6 posts

Posted 22 September 2011 - 11:21 AM

Ahh..

So simple, but yet so hard to find out

I'm just going to keep throwing qestions at you.

while(i < x){
i++;
printf("%d\n",i);}

if (i % 2 == 0);

printf("%d\n is even",i);
else
printf("%d\n is odd",i);

Two problems here, as you of course see. I need another if to get the else into the equation. And also the odd/even things only comes in the end. And not on every number. Do you have a cunning way to get around this
• 0

### #12 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 22 September 2011 - 11:39 AM

You shouldn't have a ; on the if line.
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### Also tagged with one or more of these keywords: c, nested loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download