I am stuck somewhere(have some problem with the random number) and I know it, but can't find where to fix...
Two void functions must be used; void randomNumbers(int numbers[][3], int rowSize), void randomCounts(int numbers[][3], int size, int counts[])
I can't put images to show how it should and does look like in .exe files as I just signed up today ...... Hope this works ;(
Expected Result:
//========================
// 7 6 5
// 2 1 1
// 6 7 2
// 9 3 3
// 8 1 1
//========================
//Ran. Number: 0 1 2 3 4 5 6 7 8 9
//Frequency(Counts): 0 4 2 2 0 1 2 2 1 1
What I DID:
//========================
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// 0 0 0
// ========================
// Ran. Number: 0 1 2 3 4 5 6 7 8 9
// Frequency(Counts): 001A148D
Code:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
const int COL = 3;
const int SIZE = 5;
void randomNumbers(int inumbers[][3], int rowSize) {
int num = 0;
for (int i = 0; i < 10; i++) {
num = rand() % 10;
}
}
void randomCounts(int inumbers[][3], int size, int counts[]) {
for (int i = 0; i < 10; i++) {
counts[i]++;
cout << setw(5) << counts[i];
}
}
int main(){
int random[SIZE][COL] = {};
srand((unsigned)time(NULL));
cout << endl;
cout << "==================" << endl;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < COL; j++) {
cout << setw(5) << random[i][j];
if (j == COL - 1) {
cout << endl;
}
}
}
cout << "==================" << endl;
cout << endl;
cout << "Ran. Number: " << setw(5) << "0" << setw(5) << "1" << setw(5) << "2" << setw(5) << "3" << setw(5) << "4" << setw(5) << "5" << setw(5) << "6" << setw(5) << "7" << setw(5) << "8" << setw(5) << "9" << endl;
cout << "Frequency(Counts): " << randomCounts << endl;
return 0;
}
Ok, so why are you getting 0, 0, 0.... Because you never actually call your functions. You initialize your array:
int random[SIZE][COL] = {};
Then you print it here:
cout << setw(5) << random[i][j];
And nowhere in between do you set anything into this array. When you do start calling your functions you will find they don't work, due to copying the input and doing some undefined behaviour. When you have debugged this a bit more, ask a new question.
Related
I got a simple cout statement below. And I want to print 5 lines at a time (the output below). I am new to C++. The 3 dots just a symbol to see the output
int count = 0;
cout << "Print line:" << endl;
for (int i = 0; i < 10; i++){
cout << i << endl;
count++;
if (count > 6){ //if (count % 6 == 0) this is wrong also btw
cout << i << " ..." << endl;
continue;
}
}
The output of the program above is
Print line:
0
1
2
3
4
5
6
6 ...
7
7 ...
8
8 ...
9
9 ...
Expected output
Print line:
0
1
2
3
4
5
Print line
6
7
8
9
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i % 5 == 0)
cout << "Print Line\n";
cout << i << endl;
}
}
for (int i = 0; i < 10; i++){
if (i % 6 == 0)
cout << "Print line:" << endl;
cout << i << endl;
}
You need to first check if each loop satisfies the condition i%6==0 or not and then print the number. If the condition is true, then the line will be printed.
Another way to do this to use an extra loop;
for ( int i = 0; i < 20; i += 6 )
{
for ( int n = i; n < i + 6; n++ )
{
if ( n > 20 ) break;
std::cout << n << std::endl;
}
std::cout << "print line" << std::endl;
}
I would like to store the smallest value of compute_evaluation_function in current_best_eval, and the corresponding vector that gives that smallest value in ILS_best_p, I've succeeded with the current_best_eval, but can't seem to get it for the ILS_best_p even though they're under the same if statement.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <ctime>
#include <cmath>
using namespace std;
long int **read_matrix(ifstream &input, long int n);
double ran01(long int *idum);
long int *generate_random_vector(long int n);
unsigned long int compute_evaluation_function(long int n);
void first_2_opt_symmetric (long int n);
void swap(long int pos_1, long int pos_2, long int *);
void perturbation(long int pert_size);
long int *p;//the vector generated from generate_random_vector()
long int **d;//one of the matrices read from file
long int **f;//one of the matrices read from file
long int n;
long int actual_solution_value;
long int new_eval;
long int current_eval;
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
int main()
{
string name;
long int optimum;
ifstream infile;
long int initial_eval;
long int *ILS_best_p;
long int new_vs_current;
new_vs_current = INT_MAX;
long int current_best_eval;
current_best_eval = INT_MAX;
long int new_current_eval;
long int best_eval;
long int improvement;
long int i;
long int j;
infile.open("nug12.dat");
infile >> name;
cout << "Name: " << name << "\n";
infile >> n;
cout << "Rows and Columns: " << n << "\n";
infile >> optimum;
cout << "Optimum solution: " << optimum << "\n";
d=read_matrix(infile, n);
f=read_matrix(infile, n);
cout << endl;
p=generate_random_vector(n);
compute_evaluation_function(n);
cout << endl;
first_2_opt_symmetric (n);
cout << endl;
initial_eval = compute_evaluation_function(n);
cout << endl << endl;
for (i = 0; i < 500; i++) //ILS loop
{
if (new_eval < current_eval)
{
new_vs_current = new_eval;
cout << "iteration 1st = " << i+1 << " " << endl;
}
else if (current_eval < new_eval)
{
new_vs_current = current_eval;
cout << "iteration 2nd = " << i+1 << " " << endl;
}
cout << "iteration = " << i+1 << " " << endl;
cout << " Vector before perturbation = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
perturbation(3);
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
cout << " Vector after perturbation = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
first_2_opt_symmetric (n);
new_current_eval = compute_evaluation_function(n);
cout << endl << "current best eval = " << current_best_eval << endl;
cout << endl;
cout << " Vector after local search = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
cout << " Vector p = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
if (new_current_eval < current_best_eval)
{
new_eval = new_current_eval;
cout << "iteration 3rd = " << i+1 << " " << endl;
}
else if (current_best_eval <= new_current_eval)
{
new_eval = current_best_eval;
cout << "iteration 4th = " << i+1 << " " << endl;
}
if (new_eval < new_vs_current)
{
current_best_eval = new_eval;
cout << "iteration 5th = " << i+1 << " " << endl;
ILS_best_p = p;
}
else if (new_vs_current < new_eval)
{
current_best_eval = new_vs_current;
cout << "iteration 6th = " << i+1 << " " << endl;
ILS_best_p = p;
}
cout << endl << "current best eval = " << current_best_eval << endl;
}
cout << endl << "current best eval = " << current_best_eval << endl;
cout << "ILS best vector = ";
for (i = 0; i < n; i++)
{
cout << ILS_best_p[i] << " ";
}
return 0;
}
long int **read_matrix(ifstream &input, long int n)
{
/*
FUNCTION: read instance matrices
INPUT: instance file name, instance size
OUTPUT: d-matrix and f-matrix
(SIDE)EFFECTS: none
COMMENTS: read the distance matrix and flow matrix
*/
long int i, j;
long int **matrix = new long int *[n];
for (i = 0; i < n; i++)
{
matrix[i] = new long int[n];
for (j = 0; j < n; j++)
{
if( !(input >> matrix[i][j]) )
{
cerr << "Error reading at " << i << j << endl;
exit(1);
}
}
}
return matrix;
}
double ran01(long int *idum)
{
/*
FUNCTION: returns a pseudo-random number
INPUT: a pointer to the seed variable
OUTPUT: a pseudo-random number uniformly distributed in [0,1]
(SIDE)EFFECTS: changes the value of seed
*/
long k;
double ans;
k =(*idum)/IQ;
*idum = IA * (*idum - k * IQ) - IR * k;
if (*idum < 0 )
{
*idum += IM;
}
ans = AM * (*idum);
return ans;
}
long int *generate_random_vector(long int n)
{
/*
FUNCTION: generates a random vector
INPUT: vector dimension
OUTPUT: returns pointer to vector, free memory when vector is not needed anymore
(SIDE)EFFECTS: none
*/
long int i, j, help;
long int *v;
srand(time(0));
long int seed=rand();
v = new long int[ n ];
for ( i = 0 ; i < n; i++ )
{
v[i] = i;
}
for ( i = 0 ; i < n-1 ; i++)
{
j = (long int) ( ran01( &seed ) * (n - i));
assert( i + j < n );
help = v[i];
v[i] = v[i+j];
v[i+j] = help;
}
return v;
}
unsigned long int compute_evaluation_function(long int n)
{
/*
FUNCTION: compute evaluation function
INPUT: pointer to solution
OUTPUT: evaluation function
(SIDE)EFFECTS: none
COMMENTS: none
*/
long int i, j;
unsigned long obj_f_value;
obj_f_value = 0;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
obj_f_value += d[i][j] * f[p[i]][p[j]];
}
}
cout << obj_f_value;
return obj_f_value;
}
void first_2_opt_symmetric (long int n)
{
/*
FUNCTION: first improvement 2-opt local search for symmetric instances
INPUT: pointer to some initial assignment
OUTPUT: none
(SIDE)EFFECTS: initial assignment is locally optimized, dlb is used to increase the search speed, the dlb value is changed to false if item change it's location during perturbation
COMMENT: neighborhood is scanned in random order, use of register for faster computation
*/
long int improvement = 1;
long int improve_item = 0;
long int u, v, i, j, k;
long int tmp;
long int *x;
improvement = 1;
x = generate_random_vector(n);
while (improvement)
{
improvement = 0;
for ( i = 0 ; i < n ; i++ )
{
u = x[i];
improve_item = 0;
for ( j = 0 ; j < n ; j++ )
{
v = x[j];
if (u == v)
continue;
tmp = 0;
for ( k = 0 ; k < n ; k++ )
{
if ( (k != u) && (k != v) )
{
tmp += (d[u][k] - d[v][k]) * (f[p[v]][p[k]] - f[p[u]][p[k]]);
}
}
if (tmp < 0)
{
improvement = 1;
improve_item = 1;
swap (u, v, p);
}
}
}
}
delete []x;
}
void swap(long int pos_1, long int pos_2, long int *p)
{
/*
FUNCTION: swap position of two items in a vector
INPUT: position 1, position 2, pointer to vector
OUTPUT: none
(SIDE)EFFECTS: none
COMMENTS: none
*/
long int tmp;
tmp = p[pos_1];
p[pos_1] = p[pos_2];
p[pos_2] = tmp;
}
void perturbation(long int pert_size)
{
/*
FUNCTION: apply random perturbation
INPUT: pointer to solution, perturbation scheme, perturbation strength, change in perturbation, end perturbation size
OUTPUT: none
(SIDE)EFFECTS: new solution formed from old solution
COMMENTS: none
*/
long int hold_value[n]; //allocate memory for items to be chosen in move/*
int chosen[pert_size]; //*allocate temporary memory to determine items to be moved */*
long int i;
for(i = 0; i < n; i++)
{
hold_value[i] = p[i]; //initialize hold_value with the same value from local search/*
}
int j = n - 1;
int rand_number;
int rand_no;
for(i = 1; i <= pert_size; i++)
{
rand_number = rand();
rand_no = rand_number % j; //choose random number from 1 to size - 1/
chosen[i - 1] = rand_no + i; //copy the value to chosen[i]
j--;
}
long int temp;
for(i = 0; i < pert_size; i++)
{
temp = chosen[i];
swap(i, temp, hold_value); //swap chosen[i] and hold_value[i]; n first item in hold_value[i] is the index array that will be included in perturbation/
}
long int temp1;
long int temp2;
temp1 = p[hold_value[1]];//
temp2 = p[hold_value[0]];
for(i = 0; i < pert_size - 1; i++)
{
p[hold_value[i + 1]] = temp2;
temp2 = temp1;
temp1 = p[hold_value[i + 2]];
}
p[hold_value[0]] = temp2;
actual_solution_value = compute_evaluation_function(n);
new_eval = actual_solution_value;
current_eval = new_eval;
}
This is the file I read from:
Nug12
12
578
0 1 2 3 1 2 3 4 2 3 4 5
1 0 1 2 2 1 2 3 3 2 3 4
2 1 0 1 3 2 1 2 4 3 2 3
3 2 1 0 4 3 2 1 5 4 3 2
1 2 3 4 0 1 2 3 1 2 3 4
2 1 2 3 1 0 1 2 2 1 2 3
3 2 1 2 2 1 0 1 3 2 1 2
4 3 2 1 3 2 1 0 4 3 2 1
2 3 4 5 1 2 3 4 0 1 2 3
3 2 3 4 2 1 2 3 1 0 1 2
4 3 2 3 3 2 1 2 2 1 0 1
5 4 3 2 4 3 2 1 3 2 1 0
0 5 2 4 1 0 0 6 2 1 1 1
5 0 3 0 2 2 2 0 4 5 0 0
2 3 0 0 0 0 0 5 5 2 2 2
4 0 0 0 5 2 2 10 0 0 5 5
1 2 0 5 0 10 0 0 0 5 1 1
0 2 0 2 10 0 5 1 1 5 4 0
0 2 0 2 0 5 0 10 5 2 3 3
6 0 5 10 0 1 10 0 0 0 5 0
2 4 5 0 0 1 5 0 0 0 10 10
1 5 2 0 5 5 2 0 0 0 5 0
1 0 2 5 1 4 3 5 10 5 0 2
1 0 2 5 1 0 3 0 10 0 2 0
I am a student who is new to c++ and wondering if there was a way to get my program to display more than 1 answer if they are the same. I am writing a c++ program for a class,using a two dimensional array, as well as several others to track sales amount at a car company. At the end of the program, the salesperson with the most and least amounts as well as the most amount of cars and least amount of cars are displayed. I have the program running mostly correctly, only problem I am having is getting it to display if one of the results has two answers.
My input data is as follows
Car S A L E S P E R S O N
Model 1 2 3 4 5 6 7 8 Total Average
________________________________________________________________________________________
1 8 5 2 4 1 1 1 3 ?? ??
2 5 7 2 2 3 4 3 1 ?? ??
3 5 1 1 0 0 0 5 5 ?? ??
4 2 3 2 1 2 2 2 3 ?? ??
5 7 2 0 0 0 1 4 1 ?? ??
6 2 2 2 2 3 4 3 2 ?? ??
7 2 1 2 3 3 1 2 1 ?? ??
8 5 8 1 1 2 1 2 1 ?? ??
9 4 8 2 1 1 2 3 2 ?? ??
10 1 4 4 4 2 4 2 4 ?? ??
__________________________________________________________________________
Total ?? ?? ?? ?? ?? ?? ?? ?? ??
Average ?? ?? ?? ?? ?? ?? ?? ?? ??
based on the data, car models 5 and 7 sold the least , so 2 answers need to be displayed.
based on the data sales people 1 and 2 sold the most so again there needs to be 2 answers displayed.
would anyone be able to advise me or give me a little help with this part?
// Lab09car.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ifstream InFile;
int CarModel;
int NumCarModels = 10;
double CarModelSum[10], SalesPersonSum[8];
double CarModelAvg[10], SalesPersonAvg[8];
double CarModelAverage;
int SalesPerson;
int NumSalesPerson = 8;
int CarModelCount = 0;
int MaxModel, MinModel, MaxSalesPerson, MinSalesPerson;
InFile.open("E:/CSC133-01/CSC133LabAssignments/Lab09/Lab09inp.dat");
double CarSales[12][10];
int TotalTotals{};
double TotalAverages{};
cout << "\n________________________________________________________________________________________" << endl;
cout << "\n \t\t Automobile Sales Report For The Month Of June";
cout << "\n \t\t __________________________________________________";
cout << "\n \t\t Report Prepared By: " << endl;
cout << "\n Car \t\t\t\t SALESPERSON";
cout << "\n Model \t 1\t 2\t 3\t 4\t 5\t 6\t 7\t 8\t Total\t Average";
cout << "\n________________________________________________________________________________________" << endl;
//reads data into matrix from file
for (CarModel = 0;CarModel < NumCarModels; CarModel++)
{
for (SalesPerson = 0;SalesPerson < NumSalesPerson;SalesPerson++)
{
InFile >> CarSales[CarModel][SalesPerson];
}
}
for (int i = 0;i < 10;i++)
{
CarModelSum[i] = 0;
for (int j = 0;j < 8;j++)
CarModelSum[i] += CarSales[i][j];
CarModelAvg[i] = (CarModelSum[i] * 1.0) / 8;
}
for (int i = 0;i < 8;i++)
{
SalesPersonSum[i] = 0;
for (int j = 0;j < 10;j++)
SalesPersonSum[i] += CarSales[j][i];
SalesPersonAvg[i] = (SalesPersonSum[i] * 1.0) / 10;
}
for (int i = 0;i < 10;i++)
{
cout << "\n " << (i + 1);
for (int j = 0;j < 8;j++)
cout<<setprecision(0) << "\t " << CarSales[i][j];
cout << " \t " << CarModelSum[i] << "\t " << fixed << setprecision(2) << CarModelAvg[i];
}
cout << "\n________________________________________________________________________________________" << endl;
cout << "\n Total ";
for (int i = 0;i < 8;i++)
{
cout << "\t " <<setprecision(0)<< SalesPersonSum[i];
}
for (int i = 0;i < 8;i++)
{
TotalTotals += SalesPersonSum[i];
}
cout << "\t" << setw(4)<< TotalTotals;
cout << "\n Average ";
for (int i = 0;i < 8;i++)
{
cout << fixed << setprecision(2) << SalesPersonAvg[i] << "\t ";
}
for (int i = 0;i < 8;i++)
{
TotalAverages += SalesPersonAvg[i];
}
TotalAverages = TotalAverages / 8;
cout << "\t"<<setw(5)<<TotalAverages;
MaxModel =0, MinModel = 0;
for (int i = 1;i < 10;i++)
{
if (CarModelSum[i] > CarModelSum[MaxModel])
MaxModel = i;
if (CarModelSum[i] < CarModelSum[MinModel])
MinModel = i;
}
MaxSalesPerson = MinSalesPerson = 0;
for (int i = 1;i < 8;i++)
{
if (SalesPersonSum[i] > SalesPersonSum[MaxSalesPerson])
MaxSalesPerson = i;
if (SalesPersonSum[i] < SalesPersonSum[MinSalesPerson])
MinSalesPerson = i;
}
cout << "\n________________________________________________________________________________________" << endl;
cout <<setprecision(0)<< "\n The Car Model that sold the most number of cars is the Model " << (MaxModel + 1) << ". There were " << CarModelSum[MaxModel] << " sold";
cout << "\n The Car Model that sold the least number of cars is the Model " << (MinModel + 1) << ". There were " << CarModelSum[MinModel] << " sold";
cout << "\n The Salesperson who sold the most number of cars is " << (MaxSalesPerson + 1) << ". That person sold " << SalesPersonSum[MaxSalesPerson] << " cars";
cout << "\n The Salesperson who sold the least number of cars is " << (MinSalesPerson + 1) << ". That person sold " << SalesPersonSum[MinSalesPerson] << " cars";
cout << "\n________________________________________________________________________________________" << endl;
}
Instead of storing the index of the MaxModel, store the actual value. Then, in another loop, print all models with that value.
MaxModel = CarModelSum[0], MinModel = CarModelSum[0];
for (int i = 1; i < 10;i++) {
if (CarModelSum[i] > MaxModel)
MaxModel = CarModelSum[i];
if (CarModelSum[i] < MinModel)
MinModel = CarModelSum[i];
}
Then, print the ones that match:
cout << "The Car Model(s) that sold the most number of cars: ";
for (int i = 0; i < 10;i++) {
if (CarModelSum[i] == MaxModel) {
cout << i << " "
}
}
cout << endl << "There were " << MaxModel << " sold each";
And do the same for the other values.
As far as I'm concerned, you may need two loops. For example:car models 5 and 7 sold the least , so 2 answers need to be displayed. First of all we should need to get the value "a" of the least sales . And then we will get car models 5 and 7 sold the least.
Here is a simple code: We enter a set of data ,like{1,2,3,4,3,2,5,1,2,3,}. We can get the minimum value is "1". And then we can get the sequence of corresponding numbers "1" is "0" and "7"
#include "pch.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int a[10];
int min;
int min_n;
int i;
for (i = 0; i < 10; i++)
{
cin >> a[i];
}
min = a[0];
for (i = 0; i < 10; i++)
{
if (a[i] <= min) { min = a[i]; min_n = i; }
}
cout << "min=" << min << endl;
cout << "——————————————————————" << endl;
int *p;
p = a;
for (i = 0; i < 10; i++)
{
if (*p == min)
{
cout << i << endl;
}
p++;
}
return 0;
}
Hope this code is helpful to you.
im programming a little scrabble game, here are the code which i prefer to simulate the "playfield", but i got some issue
#include "Scrabble.h"
#include <iostream>
#include <cstdlib>
#include <stdexcept>
using namespace std;
Scrabble::Scrabble() {
Character *pointer;
pointer = NULL;
reihe = 15;
spalte = 15;
for (int i = 0; i < reihe; i++) {
playground.push_back(vector <Character>());
for (int j = 0; j < spalte; j++) {
playground[i].push_back('a');
}
}
}
void Scrabble::print() {
cout << " ";
for (int i = 0; i < column; i++) {
cout << i << " ";
}
cout << endl;
for (int r = 0; r < row; r++) {
cout << r << " ";
for (int s = 0; s < cloumn; s++) {
cout << playground[r][s] << " ";
}
cout << endl;
}
}`
the output looks a little strange
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
1 a
a
some advice how i get it looks normal like:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 a
1 a
2 a
3 a
4 a
5 a
6 a
7 a
8 a
9 a
10 a
11 a
12 a
13 a
14 a
any advice will be helpful, thx
Your code snippet doesn't compile (e.g. variable cloumn), so I suppose this is not exactly the code that you are testing.
One problem you'll face, is to ensure the with of a field. For this you have to include <iomanip>:
void Scrabble::print() {
cout<<" ";
for (int i = 0; i < column; i++) {
cout << setw(2)<<i << " ";
}
cout << endl;
for (int r = 0; r < row; r++) {
cout << setw(2)<< r << " ";
for (int s = 0; s < column; s++) {
cout << setw(2)<< playground[r][s] << " ";
}
cout << endl;
}
}
Here an online version (compiled without class, for demo purpose, as I don't know the definition of Scrabble
I would like to pass a 2D array to another function. I have an example of my code that I have all in the main function. However, the assignment requires that we split the code into a total of three functions, the main, adjmatrix, and adjlist functions.
All in the main function
#include<iostream>
#include<fstream>
using namespace std;
int main(void)
{
ifstream in;
char infile[40];
int c, u, v;
cout << "Please enter the input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
while(in.fail()) {
cout << "Please enter a CORRECT input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
}
//adj matrix
cout << "Adjacency Matrix" << endl;
in >> c;
int array[c][c];
for(int i=0; i<c; i++) {
for(int j=0; j<c; j++) {
array[i][j] = 0;
}
}
while(in >> u >> v) {
array[u][v] = 1;
}
cout << c << endl;
for(int i=0;i<c;i++) {
cout << i << " ";
for(int j=0;j<c;j++){
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
//adj list
cout << "Adjacency List" << endl;
cout << c << endl;
for(int i=0;i<c;i++) {
cout << i << " --> ";
for(int j=0;j<c;j++) {
if(array[i][j] == 1) {
cout << j << " ";
}
}
cout << endl;
}
in.close();
return 0;
}
This program outputs an adjacency matrix and adjacency list from the following input file
9
2 8
0 6
8 5
2 4
3 1
2 3
4 1
6 1
2 6
7 5
1 7
The following is the output
Adjacency Matrix
9
0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1 0
2 0 0 0 1 1 0 1 0 1
3 0 1 0 0 0 0 0 0 0
4 0 1 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 1 0 0 0 0 0 0 0
7 0 0 0 0 0 1 0 0 0
8 0 0 0 0 0 1 0 0 0
Adjacency List
9
0 --> 6
1 --> 7
2 --> 3 4 6 8
3 --> 1
4 --> 1
5 -->
6 --> 1
7 --> 5
8 --> 5
I read somewhere that passing a 2D array requires the second dimension to be entered. I also read something about it having to be a global constant? So I may have gotten a little crazy and went off on the crazy path on trying some things so please excuse some of the stupidity. The problem I think I am having is the actual array size comes from the file so I dont really understand where to go for declaring an array's second dimension with an actual value when int c is initialized to the first value in the input file. The following is my failed attempt at passing a 2D array. Enjoy:
#include<iostream>
#include<fstream>
using namespace std;
const int c;
void adjmatrix(istream &in, int array[][c]);
int main(void)
{
ifstream in;
char infile[40];
cout << "Please enter the input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
while(in.fail()) {
cout << "Please enter a CORRECT input data file name(NO SPACES): ";
cin >> infile;
in.open(infile);
}
in >> c;
int array[c][c];
adjmatrix(in, array);
in.close();
return 0;
}
void adjmatrix(istream &in, int array[][c])
{
int u,v;
for(int i=0; i<c; i++) {
for(int j=0; j<c; j++) {
array[i][j] = 0;
}
}
while(in >> u >> v) {
array[u][v] = 1;
}
cout << c << endl;
for(int i=0; i<c; i++) {
cout << i << " ";
for(int j=0; j<c; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
You can allocate the array dynamically instead of statically defining it.
int i;
int **array;
in >> c;
array = new int*[c];
for(i=0;i<c;i++)
array[i] = new int[c];
adjmatrix(in, array,c);
The declaration of the function adjmatrix will be adjmatrix(istream &in, int **array,int c);
Following may help:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
void print_adjacency_matrix(const std::vector<std::vector<int>>& mat)
{
std::cout << "Adjacency Matrix" << std::endl;
std::cout << mat.size() << std::endl;
for(std::size_t i = 0; i != mat.size(); ++i) {
std::cout << i << " ";
for(auto e : mat[i]) {
std::cout << e << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void print_adjacency_list(const std::vector<std::vector<int>>& mat)
{
std::cout << "Adjacency List" << std::endl;
std::cout << mat.size() << std::endl;
for (std::size_t i = 0; i != mat.size(); ++i) {
std::cout << i << " --> ";
for(auto e : mat[i]) {
if (e == 1) {
std::cout << e << " ";
}
}
std::cout << std::endl;
}
}
int main()
{
std::ifstream in;
std::string infile;
std::cout << "Please enter the input data file name(NO SPACES): ";
std::cin >> infile;
in.open(infile);
while(in.fail()) {
std::cout << "Please enter a CORRECT input data file name(NO SPACES): ";
std::cin >> infile;
in.open(infile);
}
int c;
in >> c;
std::vector<std::vector<int> > array(c, std::vector<int>(c));
int u, v;
while(in >> u >> v) {
array[u][v] = 1;
}
in.close();
print_adjacency_matrix(array);
print_adjacency_list(array);
return 0;
}