473,395 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Print things out in a table problem

Hello everyone. This is a relatively simple program that is driving me crazy. The user enters how many verticies and how many edges. The program then asks where the edges are...in a 2D array. If the user enters a 1 (indicated there is an edge) that array element is set to 1. For some reason my print out is all wrong. It prints out to many elements, and incorrect values.

Any help is appreciated, I think I'm just missing some minor thing....but I cannot find it.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. void main ()
  5. {
  6.     int graph[10][10]={0,0}, num_vert=0, num_edge=0,r=0,c=0,cnt=0,i=0, j=0;
  7.     cout<<"Enter the number of verticies: "<<endl;
  8.     cin>>num_vert;
  9.     cout<<"Enter the number of edges: "<<endl;
  10.     cin>>num_edge;
  11.     for (i=0;i<num_edge;++i)
  12.         for (j=0;j<num_edge;++j)
  13.     {
  14.         cout<<"Is there an edge between "<<i<<" "<<j<<endl;
  15.         cin>>cnt;
  16.         if (cnt==1)
  17.             graph[i][j]=1;
  18.         else 
  19.             graph[i][j]=0;
  20.     }
  21.     i=0;
  22.     for (i=0;i<num_vert;++i)
  23.         for (j=0;j<num_vert;++j)
  24.             cout<<"GRAPH "<<i<<" "<<j<<" "<<graph[i][j]<<endl;
  25.     i=0;
  26.     do
  27.     {
  28.         cout<<"     "<<i;            // top header
  29.             ++i;
  30.     }
  31.     while(i<num_vert);
  32.     int t=0;
  33.     do                                        //output
  34.     {
  35.         cout<<endl<<endl<<t<<"|";
  36.         for (int r=0;r<num_edge;++r)
  37.             for (int c=0;c<num_edge+1;++c)
  38.                 cout<<"    "<<graph[r][c];
  39.         ++t;
  40.     }
  41.     while ( t<num_vert);
  42.     cout<<endl<<"T "<<t<<" NUM_VERT "<<num_vert<<" NUM_EDGE "<<num_edge;
  43. }
  44.  
  45.  
Thanks,
J
Dec 4 '07 #1
3 1851
Laharl
849 Expert 512MB
void main() is nonstandard and can lead to all sorts of crazy hijinx that you really don't want. Use int main() instead.

You're using ++i and ++j rather than i++ and j++. This means that at each iteration of the loop, rather than using the current value and then incrementing, it increments and uses that value. This may give you the behavior you seek, but generally the postfix (i++/j++) is preferred.
Dec 4 '07 #2
Made those 2 changes however the problem is still there.

For instance when I select graph[0][0] as a yes, so graph[0][0]=1 it prints out a 1 in all columns of 0 instead of just at 0,0.


J
Dec 4 '07 #3
Problem is fixed, i started from scratch and had it done in about 10 minutes.

Thanks to those who took a look.

J
Dec 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: weiwei | last post by:
HI I am having a problem with print friendly function with asp, I have download some code regard with print friendly, it works fine with static html information, however, I have the dynamic pages...
2
by: Yaron Cohen | last post by:
Hi, I would like to ask for your help. I am using IE5.5. I have a wide page with horizontal scroll bar. The problem is that I get only 1 page when printing it using "file->print" or...
4
by: rom | last post by:
I need to print a html table when the user clicks on a key. the problem is that i don't want the printer dialog box to appear. i guess this is impossible in javascript so i think to create an...
2
by: Bill_DBA | last post by:
I have the following stored procedure that is called from the source of a transformation in a DTS package. The first parameter turns on PRINT debug messages. The second, when equals 1, turns on the...
4
by: sara | last post by:
Hi - I've looked at many posts, and cannot find the answer on this specific problem. I have several fields on a table, which I've defined as "Text", 3 characters, Format Yes/No (I picked up the...
2
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default...
1
by: chriswilliams | last post by:
This code prints output in rows like this: ****** ****** ****** How to make print in blocks like this? ***** ***** ****** ***** ***** ****** ***** ***** ***** start= int...
3
Ganon11
by: Ganon11 | last post by:
Hey everyone, For a class assignment, I am making a class scheduling CGI program/script. I have everything working so far except actually displaying the schedule as a grid/2D table. I am not...
3
by: gggram2000 | last post by:
I'm using visual studio 2008 with a windows based project written in C# with sql server as backend; My database stores items with their respective details. I'ved created a very basic and simple...
11
by: Gord | last post by:
When I open a certain report, it runs some code that generates the records that will be displayed in that report. This works fine. When I go to print preview the report it appears that the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.