Ada susah, senang, sederhana, mengelirukan, macam2 ada... memang mencabar soalan bahasa pengaturcaraan, dan lebih mencabar apabila soalan dan jawapan in english...Miss kata standbye final exam also in english,,, pasni presentation, habiskan sukatan dan hantar asignment..
/*
* A function to print a diamond on the console
* while allowing the user to choose its size
* author: OliveOyl
*/
#include <iostream>
void printDiamond (int num)
{
//print top half of diamond
for(int i = 0; i<num; i+=2)//use i+=2 instead of i++ so that the rows will
{//follow the format row1: 1 star, row2: 3 stars, etc.
for(int j=0; j<num-i; j+=2)//update j by 2 instead of 1 because we use i in our condition
std::cout<<" ";
for(int k=0; k<=i; k++)
std::cout<<"*";
std::cout<<std::endl;
}
if(num%2==0)//check if num is even
num-=1; //if num is even, subtract 1 from it so the rows will line up correctly
num-=2;//subtract 2 from num so that the middle row will only be printed once
//print bottom half of diamond
for(int i=0;i<num;i+=2)
{
for(int j=0;j<=i+2;j+=2)
std::cout<<" ";
for(int k=1;k<=num-i;k++)
std::cout<<"*";
std::cout<<std::endl;
}
}
/** EXAMPLE USAGE **/
int main ()
{
int num;
std::cout<<"Enter a number: \n";
std::cin>>num;
printDiamond(num);
std::system("Pause");
return EXIT_SUCCESS;
}
* A function to print a diamond on the console
* while allowing the user to choose its size
* author: OliveOyl
*/
#include <iostream>
void printDiamond (int num)
{
//print top half of diamond
for(int i = 0; i<num; i+=2)//use i+=2 instead of i++ so that the rows will
{//follow the format row1: 1 star, row2: 3 stars, etc.
for(int j=0; j<num-i; j+=2)//update j by 2 instead of 1 because we use i in our condition
std::cout<<" ";
for(int k=0; k<=i; k++)
std::cout<<"*";
std::cout<<std::endl;
}
if(num%2==0)//check if num is even
num-=1; //if num is even, subtract 1 from it so the rows will line up correctly
num-=2;//subtract 2 from num so that the middle row will only be printed once
//print bottom half of diamond
for(int i=0;i<num;i+=2)
{
for(int j=0;j<=i+2;j+=2)
std::cout<<" ";
for(int k=1;k<=num-i;k++)
std::cout<<"*";
std::cout<<std::endl;
}
}
/** EXAMPLE USAGE **/
int main ()
{
int num;
std::cout<<"Enter a number: \n";
std::cin>>num;
printDiamond(num);
std::system("Pause");
return EXIT_SUCCESS;
}
No comments:
Post a Comment