Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,491 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,299 people online right now. Registration is fast and FREE... Join Now!




Diamond Problem

 
Reply to this topicStart new topic

Diamond Problem

WarGeneral
23 Mar, 2007 - 06:27 PM
Post #1

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 3


My Contributions
Write a program that reads a number and a symbol from a file (diamondData.txt) and prints a diamond constructed from the given symbol, with that many symbols at the center. It should repeat this until the number 0 is read. If an even number is read it should read the symbol next to it, but print a message.
For example if the line of data is

6 *

it should print

Can't process. Invalid data: 6 *

and go on to the next line of data. Sample input file:

5 M
11 *
6 H
7 #
0

Sample output:

M
MMM
MMMMM
MMM
M

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

Can't process. Invalid data: 6 H

#
###
#####
#######
#####
###
#

Hint: for each row

* count and print some spaces, then
* count and print some characters.



Here is my code, I have no clue what I'm doing

#include<stdio.h>
#define sentinel 0

int main(void)
{
/*Declaring variables for file streams*/


FILE* Data_in;
int number; /*number that represent the number of symbols you will use*/
char symbol;/*the type of symbol next to the the number*/
int i;/*counting the items*/
int center_symbols;/*number of symbols in the middle*/



/*open the files*/

Data_in =fopen("diamondData.txt","r"); /*input file*/

/*read first piece of data*/
fscanf(Data_in, "%d %c", &number, &symbol);

/*initialize variables*/
i = symbol;
center_sumbols = 0;
restriction = number%2;
number = 1;

while(center_symbols <= number)
{
printf(" %c ",i);
printf(" %c%c%c ",i,i,i);


}
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Diamond Problem
23 Mar, 2007 - 07:42 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
first off to check and see if the diamond can be contructed. You seem to be on top of that as restriction = number % 2 will tell you if the entered number is even or odd (restriction = 0 is number is even, 1 if odd) and since 0=false and nonezero=true in C this works out well.

something like:
if (restriction) {/*print diamond*/} else {cout <<"Can't process.";}
Should work out well.

To construct the diamonds I think a nice nested loop stucture should work. I myself would use two for-loops. The outer loop would count "number" times and the inner loop would count the number of characters that need to be printed.

I will leave the details up to you.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:36PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month