Vcoderz Community

Vcoderz Community (http://forum.vcoderz.com/index.php)
-   Computers & Information Technologies (http://forum.vcoderz.com/forumdisplay.php?f=6)
-   -   [help]c++ (http://forum.vcoderz.com/showthread.php?t=19264)

IM@D 11-01-2010 06:41 PM

[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!

Kingroudy 11-01-2010 06:46 PM

For the first one it is not working on odd integers.
use ceil(N/2)

IM@D 11-01-2010 06:51 PM

Quote:

Originally Posted by Kingroudy (Post 224187)
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?!

Kingroudy 11-01-2010 07:03 PM

no no no, lol, i will correct it, you need to take the ceiling not the floor.
i think you will need cmath.h

IM@D 11-01-2010 07:21 PM

Quote:

Originally Posted by Kingroudy (Post 224191)
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

:S

Google 11-02-2010 03:52 AM

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 11-02-2010 04:04 AM

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");
}}

IM@D 11-02-2010 06:50 PM

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


All times are GMT +1. The time now is 07:12 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger