(C++) Dijkstra's Algorithm Backtracking issues - c++

I'm programming Djikstra's algorithm in C++ and I'm getting the correct distances from the source node to the end node but i'm having trouble backtracking the previous nodes visited. It's giving me sort of the correct answer but not the correct answer. Also I noticed with different input data of 1 as the start node and 16 as the finish node that my algorithm is using path's that aren't allowed (it goes from 1 -> 10 -> 8 when 8 isn't allowed) but that could just be me getting path backtracking wrong.
http://pastebin.ca/3188762 - Input data (1st = max nodes and then nodes(node num, x,y) then max edges then all edges with the last line being the start and finish node)
http://textuploader.com/awp89 - Output in console
Code:
#include<iostream>
#include<fstream>
using namespace std;
struct Node
{
int nodeNum;
double x, y;
};
void dji(double map[50][50],int startNode,int endNode,int maxNodes);
int main()
{
int tempA, tempB, maxNodes, maxEdges, startNode, endNode;
double tempD;
double map[50][50];
ifstream fin;
fin.open("ass03.txt");
if(fin.good())
{
fin >> maxNodes;
Node allNodes[maxNodes];
for(int i = 0; i < maxNodes; i++)
{
for(int k = 0; k < maxNodes; k++)
{
map[i][k] = -1;
}
map[i][i] = 0;
}
for(int i = 0; i < maxNodes; i++)
{
fin >> allNodes[i].nodeNum >> allNodes[i].x >> allNodes[i].y;
}
fin >> maxEdges;
for(int i = 0; i < maxEdges; i++)
{
fin >> tempA >> tempB >> tempD;
map[tempA-1][tempB-1] = tempD;
map[tempB-1][tempA-1] = tempD;
}
fin >> startNode >> endNode;
cout << "\t";
for(int i = 0; i < maxNodes; i++)
{
cout << i+1 << "\t";
}
cout << endl;
for(int i = 0; i < maxNodes; i++)
{
cout << i+1 << "\t";
for(int k = 0; k < maxNodes; k++)
{
cout << map[i][k] << "\t";
}
cout << endl;
}
dji(map, startNode-1, endNode-1, maxNodes);
}
else
{
cout << "Incorrect filename" << endl;
}
return 0;
}
void dji(double map[50][50], int startNode,int endNode,int maxNodes)
{
int Intersections[maxNodes], path[maxNodes], temp; // equate for actual endNode
double Distances[maxNodes];
for(int a = 0; a < maxNodes; a++)
{
Intersections[a] = a;
Distances[a] = map[startNode][a];
if(map[startNode][a] != -1)
{
path[a] = startNode;
}
else
{
path[a] = -1;
}
}
Intersections[startNode] = -1;
Distances[startNode] = 0;
double minValue = 99999;
int minNode = 0;
for(int l = 0; l < maxNodes; l++)//loop max amount of times to avoid having to function loop (disconsider l = startNode)?
{
for (int i = 0; i < maxNodes; i++)
{
if(Intersections[i] == -1)
{
continue;
}
if(Distances[i] > 0 && Distances[i] < minValue)
{
minValue = Distances[i];
minNode = i;
}
}
if(Intersections[minNode] == endNode)
{
temp = l;
}
Intersections[minNode] = -1;
cout << " --- Used Node - " << minNode+1 << endl;
for(int i = 0; i < maxNodes; i++)
{
cout << Intersections[i] << " ";
}
cout << endl;
for(int i = 0; i < maxNodes; i++)
{
if(map[minNode][i] < 0)
{
continue;
}
if(Distances[i] < 0)
{
Distances[i] = minValue + map[minNode][i];
path[i] = minNode;
continue;
}
if((Distances[minNode] + map[minNode][i]) < Distances[i])
{
Distances[i] = minValue + map[minNode][i];
path[i] = minNode;
}
}
minValue = 99999;
}
for(int i = 0; i < maxNodes; i++)
{
cout << "Node:" << i+1 << " - PATH= " << path[i] << " distance = " << Distances[i] << endl;
}
cout << "Additional nodes used: " << temp << endl;
temp = path[endNode];
for(int i = 0; i < 4; i++)
{
cout << temp << " ";
temp = path[temp];
}
/*temp = path[endNode];
int temp2 = path[endNode];
for(int i = 0; i < maxNodes; i++)
{
if(i == 0)
{
cout << endNode << " ";
cout << temp << " ";
}
if(i%2 == 0)
{
if(temp != endNode)
{
temp = path[temp2];
cout << temp << " ";
}
else
{
cout << temp << " ";
i = maxNodes;
}
}
else
{
if(temp2 != endNode)
{
temp2 = path[temp]-1;
cout << temp2 << " ";
}
else
{
cout << temp2 << " ";
i = maxNodes;
}
}
}*/
//cout << "PATH = " << endNode << " < - " << path[endNode] << " < - " << path[path[endNode]-1] << " < - " << path[path[path[endNode]-1]-1] << endl;
//cout << "TEST" << path[4] << " " << path[8] << " " << path[16] << " " << endl;
}
Thank you for any help

The problem is you're mixing zero-based and one-based indexing. Your vertices are numbered 1-20, and those are the numbers that end up in your path array with valid indices 0-19. You then use the vertex number as the index into the array.
Change your code to either consistently use the vertex number, or consistently use the array index.

Related

Display first even and then odd elements in a C++array

I'm a C++ newb. I need to insert numbers to an array and then display first the odd numbers and then the even numbers in a single array. I've managed to create two separate arrays with the odd and even numbers but now I don't know how to sort them and put them back in a single array. I need your help to understand how to do this with basic C++ knowledge, so no advanced functions. Here's my code:
#include <iostream>
using namespace std;
int main()
{
int N{ 0 }, vector[100], even[100], odd[100], unify[100], i{ 0 }, j{ 0 }, k{ 0 };
cout << "Add the dimension: " << endl;
cin >> N;
cout << "Add the elements: " << endl;
for (int i = 0; i < N; i++) {
cout << "v[" << i << "]=" << endl;
cin >> vector[i];
}
for (i = 0; i < N; i++) {
if (vector[i] % 2 == 0) {
even[j] = vector[i];
j++;
}
else if (vector[i] % 2 != 0) {
odd[k] = vector[i];
k++;
}
}
cout << "even elements are :" << endl;
for (i = 0; i < j; i++) {
cout << " " << even[i] << " ";
cout << endl;
}
cout << "Odd elements are :" << endl;
for (i = 0; i < k; i++) {
cout << " " << odd[i] << " ";
cout << endl;
}
return 0;
}
If you don't need to store the values then you can simply run through the elements and print the odd and the even values to different stringstreams, then print the streams at the end:
#include <sstream>
#include <stddef.h>
#include <iostream>
int main () {
std::stringstream oddStr;
std::stringstream evenStr;
static constexpr size_t vecSize{100};
int vec[vecSize] = {10, 5, 7, /*other elements...*/ };
for(size_t vecIndex = 0; vecIndex < vecSize; ++vecIndex) {
if(vec[vecIndex] % 2 == 0) {
evenStr << vec[vecIndex] << " ";
} else {
oddStr << vec[vecIndex] << " ";
}
}
std::cout << "Even elements are:" << evenStr.rdbuf() << std::endl;
std::cout << "Odd elements are:" << oddStr.rdbuf() << std::endl;
}
Storing and sorting the elements are always expensive.
Basically, it would be better to sort them first.
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
int mergedArrays[5];
int evenNumbers[5];
int oddNumbers[5];
for(int i=0;i<5;i++){
cin>>numbers[i];
}
int temp=numbers[0];
//bubble sort
for(int i = 0; i<5; i++)
{
for(int j = i+1; j<5; j++)
{
if(numbers[j] < numbers[i])
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
int nEvens=0;
int nOdds=0;
for(int i = 0; i<5; i++)
{
if(numbers[i]%2==0)
{
evenNumbers[nEvens]=numbers[i];
nEvens++;
}
else if(numbers[i]%2!=0)
{
oddNumbers[nOdds]=numbers[i];
nOdds++;
}
}
int lastIndex=0;
//copy evens
for(int i = 0; i<nEvens; i++)
{
mergedArrays[i]=evenNumbers[i];
lastIndex=i;
}
//copy odds
for(int i =lastIndex; i<nOdds; i++)
{
mergedArrays[i]=oddNumbers[i];
}
return 0;
}
If you have to just output the numbers in any order, or the order given in the input then just loop over the array twice and output first the even and then the odd numbers.
If you have to output the numbers in order than there is no way around sorting them. And then you can include the even/odd test in the comparison:
std::ranges::sort(vector, [](const int &lhs, const int &rhs) {
return ((lhs % 2) < (rhs % 2)) || (lhs < rhs); });
or using a projection:
std::ranges::sort(vector, {}, [](const int &x) {
return std::pair<bool, int>{x % 2 == 0, x}; });
If you can't use std::ranges::sort then implementing your own sort is left to the reader.
I managed to find the following solution. Thanks you all for your help.
#include <iostream>
using namespace std;
int main()
{
int N{0}, vector[100], even[100], odd[100], merge[100], i{0}, j{0}, k{0}, l{0};
cout << "Add the dimension: " << endl;
cin >> N;
cout << "Add the elements: " << endl;
for (int i = 0; i < N; i++)
{
cout << "v[" << i << "]=" << endl;
cin >> vector[i];
}
for (i = 0; i < N; i++)
{
if (vector[i] % 2 == 0)
{
even[j] = vector[i];
j++;
}
else if (vector[i] % 2 != 0)
{
odd[k] = vector[i];
k++;
}
}
cout << "even elements are :" << endl;
for (i = 0; i < j; i++)
{
cout << " " << even[i] << " ";
cout << endl;
}
cout << "Odd elements are :" << endl;
for (i = 0; i < k; i++)
{
cout << " " << odd[i] << " ";
cout << endl;
}
for (i = 0; i < k; i++)
{
merge[i] = odd[i];
}
for (int; i < j + k; i++)
{
merge[i] = even[i - k];
}
for (int i = 0; i < N; i++)
{
cout << merge[i] << endl;
}
return 0;
}
You can use Bubble Sort Algorithm to sort whole input. After sorting them using if and put odd or even numbers in start of result array and and others after them. like below:
//Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
// Last i elements are already
// in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
// Insert In array
int result[100];
if(odd[0]<even[0])
{
for (int i = 0; i < k; i++)
{result[i] = odd[i];}
for (int i = 0; i < j; i++)
{result[i+k] = even[i];}
}else
{
for (int i = 0; i < j; i++)
{result[i] = even[i];}
for (int i = 0; i < k; i++)
{result[i+k] = odd[i];}
}

C++ Out of Range Error

Okay so I keep getting this error when I try to run my code and can't figure out how to fix it.
(Unhandled exception at 0x75195608 in hw6.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0101F850.)\
I have included my source code below. Also the file that I am reading from is not long at all so I don’t think that’s the problem.
int main() {
//Initializes all of the variables, strings,boolean, and vectors
ifstream inFS;
int count = 1;
int i = 0;
int j = 0;
int location = 0;
char ch;
bool marker = 0;
string filename = "hw6-Fall2017.txt";
string list = "ABCDEFGHIJKEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
vector<int> locations;
vector<int> find_Locations;
vector<char> notFound;
vector<char> Found;
//Iterates through the file searching for each of the characters
while (count <= 62) {
inFS.open(filename);
if (!inFS.is_open()) {
cout << "Could not open the file: " << filename << endl;
return 1;
}
while (inFS.get(ch) && marker == 0) {
location++;
if (ch == list[i]) {
marker = 1;
}
}
inFS.close();
//Sets characters not found to have a location of 0
if (marker == 0) {
location = 0;
}
locations.push_back(location);
marker = 0;
location = 0;
i++;
count++;
}
//Creates a table printing out the characters and their susequent locations
for (i = 0;i < list.size();i++) {
if (locations.at(i) == 0) {
cout << list[i] << " " << setw(6) << "NotFnd"<< " ";
notFound.push_back(list[i]);
}
else {
cout << list[i] << " " << setw(6) << locations.at(i) << " ";
find_Locations.push_back(locations.at(i));
}
j++;
if (j == 5) {
cout << endl;
j = 0;
}
}
cout << endl << endl << endl;
//Sorts the characters in the order that they were found
sort(find_Locations.begin(), find_Locations.end());
for (i = 0;i < find_Locations.size();i++) {
for (j = 0;j < locations.size();j++) {
if (find_Locations.at(i) == locations.at(j) && marker == 0) {
Found.push_back(list[j]);
j = locations.size();
}
}
}
count = 0;
j = 0;
//Creates a table printing out the characters in the oreder they were found
//in the text file along with their locations. Characters not found are
//displayed first with a location of "NotFnd".
for (i = 0;i < (Found.size() + notFound.size());i++) {
if (i < Found.size()) {
cout << Found.at(i) << " " << setw(6) << find_Locations.at(i)<< " ";
}
else {
cout << notFound.at(j) << " " << setw(6) << "NotFnd" << " ";
j++;
}
count++;
if (count == 5) {
cout << endl;
count = 0;
}
}
system("pause");
return 0;
}
The answer is not easy to find with code review.
But this line looks nice in itself
string list = "ABCDEFGHIJKEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
but together with this
while (count <= 62) {
It looks suspicious, I think it should have been
while (count < list.size()) { // 2*26+10==62
An "Off by one error" which could cause a problem here
for (i = 0;i < find_Locations.size();i++) {
for (j = 0;j < locations.size();j++) {
if (find_Locations.at(i) == locations.at(j) && marker == 0) {
Found.push_back(list[j]); // <--- if J>=list.size()
j = locations.size();
}
}
}
And a potential crash at the marked line.
But the real error is here
Found.push_back(list[j]); // j should have been i
Which should cause a crash at
cout << Found.at(i) << " " << setw(6) << find_Locations.at(i)<< " ";

power function no scope in function

#include<iostream>//Pls note:Only header allowed...
As this is the C++ i dont think so any other header is needed for that math function.
using namespace std;
int comparator(int audience[][2], int index1, int index2) {
int b1, e1;
int b2, e2;
b1 = audience[index1][1];
e1 = audience[index1][2];
b2 = audience[index2][1];
e2 = audience[index2][2];
double re1;
re1 = pow(b1, e1);
cout << re1 << endl;
double re2 = pow(b2, e2);
cout << re2 << endl;
if (re1 == re2)
{
return 0;
}
if (re1 > re2)
{
return -1;
}
if (re1 < re2)
{
return 1;
}
}
//Nothing has to be done with the rest of the two functions.
void sorting(int audience[][2], int N, int &i_index, int &j_index)
{
int i, j, temp;
for (i = 0; i<N - 1; i++)
{
if (audience[i][2] < audience[i + 1][2])
continue;
else
i_index = i;
break;
}
for (i = N; i > 1; i++)
{
if (audience[i][2]>audience[i - 1][2])
continue;
else
j_index = i;
break;
}
for (i = i_index + 1; i < j_index - 1; i++)
{
min = audience[i_index + 1][2];
for (i = )
if (audience[i_index][1] > audience[i_index + 1][1])
{
temp = audience[i_index + 1][1];
audience[i_index + 1][1] = audience[i_index][1];
audience[i_index][1] = temp;
}
}
for (i = i_index + 1; i <= j_index - 1; i++)
{
min = audience[i][2];
for (j = i_index + 2; j <= j_index - 1; j++)
{
if (min > audience[j][2])
{
temp = audience[i_index + 2][2];
audience[i_index + 1][2] = audience[i_index][2];
audience[i_index][2] = temp;
}
}
}
}
void merge(int audience[][2], int mergedarray[][2], int N, int i_index, int j_index)
{
}
int main()
{
int audience[100][2], mergedmarks[100][2];
int i, N;
int index1 = 0, index2 = 0;
int comp_result;
cout << "Enter the value of N : ";
cin >> N; // Enter size of the table
cout << "Enter the base and exponent for " << N << "rows " << endl;
for (i = 0; i < N; i++)
cin >> audience[i][0] >> audience[i][1]; //Enter numbers in the table
cout << endl << "Checking Function 1: Compare ecodes for 5 index pairs" << endl;
for (i = 0; i < 5; i++)
{
cout << "\nEnter indices of row1 and row2 that you want to compare: ";
cin >> index1 >> index2;
if (index1 < 0 || index2 < 0 || index1 >= N || index2 >= N)
continue;
comp_result = comparator(audience, index1, index2);
if (comp_result == -1)
cout << "ecode of index 1 is greater than ecode of index2" << endl;
else if (comp_result == 1)
cout << "ecode of index 1 is less than ecode of index2" << endl;
else if (comp_result == 0)
cout << "ecode of index 1 is equal to ecode of index2" << endl;
}
cout << endl;
int index_i = 0, index_j = N;
sorting(audience, N, index_i, index_j);
cout << "Checking Function 2: Printing sorted array " << endl;
for (i = 0; i < N; i++)
cout << audience[i][0] << " " << audience[i][1] << endl;
cout << endl;
cout << "index i: " << index_i << "\nindex j: " << index_j << endl;
cout << endl << "Checking Function 3: Printing Merged Array " << endl;
merge(audience, mergedmarks, N, index_i, index_j);
int merge_array_size = index_i + (N - (index_j + 1));
for (i = 0; i < N; i++)
cout << mergedmarks[i][0] << " " << mergedmarks[i][1] << endl;
cout << endl;
return 0;
}
This is the whole problem. I have to still edit the merge function. That is the whole problem.This is all.
You need to include the pow header, which is math.h, in order to use it.
add at the beginning of your file:
#include <math.h>
#include <iostream>
using namespace std;
POW is declared in math.h header file so use
#include<math.h>

Having problems with creating a merge sort algorithm (C++)

I'm having issues with the final implementation of merge sort. Below is my code:
#include <iostream>
using namespace std;
void decomposeArray(int* array, int length) {
int arrayOfArrays[length];
cout << endl;
for(int i = 0; i < length; i++) {
cout << "Array: " << array[i] << endl;
}
if (length > 1) {
int midpoint = length/2;
int elemInFirst = midpoint;
int elemInSecond = elemInFirst;
if ((length % 2) == 1) {
elemInSecond += 1;
}
int firstArray[elemInFirst];
int secondArray[elemInSecond];
for(int i = 0; i < elemInFirst; i ++) {
firstArray[i] = array[i];
}
for(int j = elemInFirst; j < length; j++) {
secondArray[j-elemInFirst] = array[j];
}
for(int i = 0; i < elemInFirst; i++) {
cout << "First half: " << firstArray[i] << endl;
}
for(int i = elemInFirst; i < length; i++) {
cout << "Second half: " << secondArray[i-elemInFirst] << endl;
}
decomposeArray(firstArray, elemInFirst);
decomposeArray(secondArray, elemInSecond);
int* superiorArray;
int* inferiorArray;
int dominantIndex;
int inferiorIndex;
if (elemInFirst > elemInSecond) {
dominantIndex = elemInFirst;
superiorArray = firstArray;
inferiorIndex = elemInSecond;
inferiorArray = secondArray;
}
else {
dominantIndex = elemInSecond;
superiorArray = secondArray;
inferiorIndex = elemInFirst;
inferiorArray = firstArray;
}
for(int i = 0; i < dominantIndex; i++) {
for (int j = 0; j < inferiorIndex; j++) {
if (superiorArray[i] > inferiorArray[j]) {
int tempVal = array[i];
array[i] = inferiorArray[j];
array[(dominantIndex+j)] = tempVal;
}
else if (j == (inferiorIndex-1)){
int tempVal = array[i];
cout << "Array at i is " << array[i] << endl;
cout << "i is " << i << endl;
cout << "This temp value is " << array[i] << endl;
array[i] = superiorArray[i];
cout << "superiorArray[i]: " << superiorArray[i] << endl;
array[((dominantIndex)+j)] = tempVal;
}
else {
continue;
}
}
cout << "Current array " << endl;
for(int i = 0; i < length; i++) {
cout << array[i] << ",";
}
cout << endl;
}
/*while ((firstIterator != elemInFirst) && (secondIterator !=elemInSecond) && (origArrayIter != length)) {
if (firstArray[firstIterator] > secondArray[secondIterator]) {
array[origArrayIter] = secondArray[secondIterator];
array[origArrayIter + 1] = firstArray[firstIterator];
firstIterator++;
secondIterator++;
origArrayIter+= 2;
}
else {
array[origArrayIter] = firstArray[firstIterator];
array[origArrayIter+1] = secondArray[secondIterator];
firstIterator++;
secondIterator++;
origArrayIter+= 2;
}
}*/
cout << endl;
for(int i = 0; i < length; i++) {
cout << "Array coming out: " << array[i] << endl;
}
/*int tempArray[length];
for(int i = 0; i < length; i++) {
tempArray[i] = array[i];
}
return tempArray;*/
}
}
Specifically, the snippet that I'm having trouble with is merging the arrays back together:
for(int i = 0; i < dominantIndex; i++) {
for (int j = 0; j < inferiorIndex; j++) {
if (superiorArray[i] > inferiorArray[j]) {
int tempVal = array[i];
array[i] = inferiorArray[j];
array[(dominantIndex+j)] = tempVal;
}
else if (j == (inferiorIndex-1)){
int tempVal = array[i];
cout << "Array at i is " << array[i] << endl;
cout << "i is " << i << endl;
cout << "This temp value is " << array[i] << endl;
array[i] = superiorArray[i];
cout << "superiorArray[i]: " << superiorArray[i] << endl;
array[((dominantIndex)+j)] = tempVal;
}
else {
continue;
}
}
cout << "Current array " << endl;
for(int i = 0; i < length; i++) {
cout << array[i] << ",";
}
cout << endl;
}
When I use an array of values {5,3,8,2}, I am returned an array {2,5,3,3}. The step before the final array produces an array {2,3,8,5}. For some reason, I just don't know why the 5 and 8 aren't swapping. Any help or guidance would be appreciated, thank you.

Printin a X with *

i wanna print a X with * , i have done the left side of the X but i don't know how to print the other side (flip/mirror) .
if you run this codes it will print just the left side of (X) and now i wanna print the right side of (X) ? so what should i do to complete the (X) using stars(*)? thank you guys.
i was wondering is it possible to do this?(i'm a newbie to programming)
#include <iostream>
// Expected output pattern:
//
// * *
// * *
// * *
// *
// * *
// * *
// * *
using namespace std;
int main() {
cout << "Printing X with star(*)" << endl;
cout << endl;
int i;
int p;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
return 0;
}
You're on the right track, to do the right hand side you have to print more **** on each line in addition to what you already have done. It might help to think of printing each line of the X as printing some **** then some spaces then more **** and reduce the number of spaces each time you get closer to the cross-over point. Does that make sense? This might help get you further (. = space):
*......*
.*....*
..*..*
...**
and so on
This is one of many ways you could get there:
int main()
{
int size = 8;
int spacesBefore;
int spacesBetween = size;
int numStars = 1;
// Top half:
int i, j;
for ( i = 0; i < size/2; i++ ) {
spacesBetween = size - ( 2 * ( i + 1 ) );
spacesBefore = i;
for ( j = 0; j < spacesBefore; j++ ) // before
cout << " ";
for ( j = 0; j < numStars; j++ ) // * left
cout << "*";
for ( j = 0; j < spacesBetween; j++ ) // between
cout << " ";
for ( j = 0; j < numStars; j++ ) // * right
cout << "*";
cout << endl;
}
// bottom half, do the same kind of thing but changing the spacings
// ...
}
ok thank you every one that helped me , i found the answer i wanted after almost 6 hours and here is the answer:
#include <iostream>
using namespace std;
int main() {
cout << "Printing X with stars" << endl;
cout << endl;
int i;
int p;
int k;
int s;
int count = 72;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++){
cout << " ";
}
count-=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
}
count = 0;
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++) {
cout << " ";
}
count +=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
if (count == 80) break;
}
return 0;
}