Vcoderz Community
We create websites that have it all, beauty & brains
Lebanon Web Design & Development - Coddict
 

Go Back   Vcoderz Community > Computer Zone > Computers & Information Technologies

Notices

Computers & Information Technologies « Everything related to computers and internet. »

Reply
 
Share Thread Tools Search this Thread
Old 11-01-2010   #1
IM@D
Registered Member
 
IM@D's Avatar
 
Last Online: 05-28-2013
Join Date: Jun 2006
Posts: 781
Thanks: 168
Thanked 303 Times in 187 Posts
Groans: 23
Groaned at 22 Times in 18 Posts
Default [help]c++

Assignment 4.1

1) Write a program that uses one loop and one if-else statement, such that given an input N, it prints N integers following the pattern: 1 2 3… N/2 (N/2-1) (N/2-2) (N/2-3)… 3 2 1, for example, if N = 11 it prints: 1 2 3 4 5 6 5 4 3 2 1, for N = 10 it prints: 1 2 3 4 5 5 4 3 2 1
2) Use two nested loops to print the following triangle:
**********
********
******
****
**
*
Ask the user to enter the width of the triangle, for example, for the above triangle, the user would have entered 10. No need to ask for the height since for each line you need to remove one * from the left and another from the right.







This is my assignment, we usually solve it in the lab course but this week we have the midterms before the lab so we won't be able to solve them.
I already wrote them codes:
For the first one it is not working on odd integers.
For the second one , it's not giving me 1 star in the end plus I can't make them centered.

Any idea guys? bkoun mamnounkoun!


IM@D is offline   Reply With Quote
Old 11-01-2010   #2
Kingroudy
Super Moderator
 
Kingroudy's Avatar
 
Last Online: 02-16-2022
Join Date: May 2006
Posts: 5,580
Thanks: 1,888
Thanked 2,653 Times in 1,593 Posts
Groans: 55
Groaned at 35 Times in 32 Posts
Default

For the first one it is not working on odd integers.
use ceil(N/2)
__________________
click on 'Groan' to switch to my left testicle.

Last edited by Kingroudy; 11-01-2010 at 07:03 PM.
Kingroudy is offline   Reply With Quote
Old 11-01-2010   #3
IM@D
Registered Member
 
IM@D's Avatar
 
Last Online: 05-28-2013
Join Date: Jun 2006
Posts: 781
Thanks: 168
Thanked 303 Times in 187 Posts
Groans: 23
Groaned at 22 Times in 18 Posts
Default

Quote:
Originally Posted by Kingroudy View Post
For the first one it is not working on odd integers.
use floor(N/2)
what is it supposed to do?!
if N=11 , N/2=5.5 it will take it 6?!
does it need a special library in the header?!
IM@D is offline   Reply With Quote
Old 11-01-2010   #4
Kingroudy
Super Moderator
 
Kingroudy's Avatar
 
Last Online: 02-16-2022
Join Date: May 2006
Posts: 5,580
Thanks: 1,888
Thanked 2,653 Times in 1,593 Posts
Groans: 55
Groaned at 35 Times in 32 Posts
Default

no no no, lol, i will correct it, you need to take the ceiling not the floor.
i think you will need cmath.h
__________________
click on 'Groan' to switch to my left testicle.
Kingroudy is offline   Reply With Quote
Old 11-01-2010   #5
IM@D
Registered Member
 
IM@D's Avatar
 
Last Online: 05-28-2013
Join Date: Jun 2006
Posts: 781
Thanks: 168
Thanked 303 Times in 187 Posts
Groans: 23
Groaned at 22 Times in 18 Posts
Default

Quote:
Originally Posted by Kingroudy View Post
no no no, lol, i will correct it, you need to take the ceiling not the floor.
i think you will need cmath.h
oke 3melta hek

Quote:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void main()
{
int i,m;
float N;
cin>>N;
m=0;
for (i=1;i<=N;i++)
{

if (i<=ceil(N/2))
{cout<<i<<" ";
m++;}
else
cout<<m--<<" ";
}

}
for N=10 3am ta3tine l result l matloub 1 2 3 4 5 5 4 3 2 1
bas for N=11 saret ta3tine 1 2 3 4 5 6 6 5 4 3 2
ana bade yeha ta3tine 1 2 3 4 5 6 5 4 3 2 1

IM@D is offline   Reply With Quote
Old 11-02-2010   #6
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

I coded it in C
Quote:
#include <stdio.h>
void main() {
int N,i;
scanf("%d",&N);
for(i=0; i<N; i++) {
if(i <= N/2-1) { printf("%d ",i+1); }
else { printf("%d ",N-i); }
}}
__________________

Google is offline   Reply With Quote
Old 11-02-2010   #7
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default

And that's for the second one
Quote:
#include <stdio.h>
void main() {
int i,j;
for(i=0; i<6; i++) {
if(i==5) { printf("*"); }
for(j=0; j<10-((i)*2); j++) {
printf("*");
}
printf("\n");
}}
__________________

Google is offline   Reply With Quote
Old 11-02-2010   #8
IM@D
Registered Member
 
IM@D's Avatar
 
Last Online: 05-28-2013
Join Date: Jun 2006
Posts: 781
Thanks: 168
Thanked 303 Times in 187 Posts
Groans: 23
Groaned at 22 Times in 18 Posts
Default

Thank you google,
I translated the first one to C++ and i modified something and it worked, merci.
but about the second, he is asking us to use only two nested loops with no if statement and he want us to remove 1 star from the right and one from the left ( it did not printed as the right format in the thread)

thanks again
IM@D is offline   Reply With Quote
Reply

  Vcoderz Community > Computer Zone > Computers & Information Technologies

Tags
helpc



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:41 AM.


Lebanon web design and development
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Share