Simple graph operations in C ++

Source: Internet
Author: User

[Cpp]
/* (1) assign a random number of less than 50 to the data in the two-dimensional array (8*8 ).
(2) design the function out () to output data in two-dimensional arrays by rows and columns;
(3) design function outDiagonal () outputs the values of elements from the top left to the lower right corner.
(4) input a position on the keyboard and output the data in the eight grids around it. (Note: there are eight entries in total, excluding the number at the position) calculate and output the sum of these numbers. In addition, if the selected position is on the edge or corner, the number of cells around is less than eight, and the output is based on the actual number. Please list each value in a regular cycle, and do not list them one by one in a sequential structure. This function is completed by the function mine.
(5) Design Function change () to change the value in the array. The changed rule is: from row 2nd (Row a [1]) to the last row, each element is the sum of the elements above it and the elements above the right, for example, a [1] [0] takes the sum of a [0] [0] And a [0] [1, a [1] [1] takes the sum of a [0] [1] and a [0] [2 ,....... No data exists in the upper-right corner of the last column of each row, for example, a [1] [7] takes the sum of a [0] [7] And a [0] [0. The elements of a [1] rows are 50 27 41 30 33 63 60 64, and a [2] rows are 77 68... 114.
*/
# Include <iostream>
# Include <ctime>
# Include <iomanip>
Using namespace std;
Void main ()
{Int a [10] [10];
Void setdata (int a [10] [10]);
Void out (int a [10] [10]);
Void outDiagonal (int a [10] [10]);
Void mine (int a [10] [10]);
Void change (int a [10] [10]);
Setdata ();
Out ();
OutDiagonal ();
Mine ();
Change ();
Out ();
}
Void setdata (int a [10] [10]) // assign a value to the graph
{
For (int k = 0; k <10; k ++) // fill the surrounding area with 0 and do not print
{A [k] [0] = 0;
A [0] [k] = 0;
A [9] [k] = 0;
A [k] [9] = 0;
}
Int I, j;
Srand (time (NULL); // adjust the random value with the previous time
For (I = 1; I <9; I ++)
For (j = 1; j <9; j ++)
A [I] [j] = rand () % 50 + 1;
}
Void out (int a [10] [10]) // output
{
For (int I = 1; I <9; I ++)
{
For (int j = 1; j <9; j ++)
Cout <left <setw (6) <a [I] [j];
Cout <endl;
}
}
Void outDiagonal (int a [10] [10]) // output diagonal Value
{
Cout <"Left diagonal:" <endl;
For (int I = 1; I <9; I ++)
Cout <setw (4) <a [I] [I];
Cout <endl <"right diagonal:" <endl;
For (I = 8; I> = 1; I --)
Cout <setw (4) <a [9-i] [I];
Cout <endl;
}
Void mine (int a [] [10]) // output the surrounding Value
{
Int x, y, sum = 0;
Cout <"coordinates of the input number" <endl;
Cin> x> y;
For (int I = X-1; I <x + 2; I ++)
For (int j = Y-1; j <y + 2; j ++)
Sum + = a [I] [j];
Sum-= a [x] [y];
Cout <sum <endl;
 
}
Void change (int a [10] [10]) // change
{
For (int I = 2; I <9; I ++)
{
For (int j = 1; j <8; j ++)
A [I] [j] = a [I-1] [j] + a [I-1] [j + 1];
A [I] [8] = a [I-1] [8] + a [I-1] [1];
}
 
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.