Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Programming homework help

c nested loop

  • Please log in to reply
16 replies to this topic

#1 flakstad

flakstad

    CC Lurker

  • Just Joined
  • Pip
  • 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! :thumbup1:

If you answer this please write which of the 9 questions you answer.

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.
added code tags

  • 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

What are your proposed answers?
  • 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
  • PipPipPipPipPipPipPip
  • 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

ti-99-sig.png
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


#4 flakstad

flakstad

    CC Lurker

  • Just Joined
  • Pip
  • 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

1) you didn't copy all of your answer
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

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 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.


#7 flakstad

flakstad

    CC Lurker

  • Just Joined
  • Pip
  • 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
int answer

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

(i*t ="%d", &answer);
prinft("Answer is &answer\n");
}
  • 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

answer = i*t;
printf("answer is %d",answer);
  • 0

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

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


#9 flakstad

flakstad

    CC Lurker

  • Just Joined
  • Pip
  • 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/


#11 flakstad

flakstad

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 22 September 2011 - 11:21 AM

Ahh..

So simple, but yet so hard to find out :mad:

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 :crying:
  • 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