• 0

C++ help me! array reverse


Question

Hello! i want a help in c++. I have to reverse one table but on the output the empty fills have a random value. I have to assign an empty value but i dont know how. here is my code

include <stdio.h>

#include <iostream>

#include <conio.h>

using std::cin;

void printRTable(char array[20][20], int n) {

int x, i;

printf ("Printing Reverse Table\n");

for (i=1;i<=n;i++) {

for (x=n;x>=1;--x) {

printf(" %d",array[x]);

}

printf("\n");

}

}

void printTable(char array[20][20], int n) {

int x, i, temp;

printf ("Printing table\n");

for (x=1;x<=n;x++) {

for (i=1;i<=x;i++) {

temp = i * x;

array[x] = temp;

printf(" %d",temp);

}

printf("\n");

}

printf("\n");

printRTable(array,n);

}

int main() {

int N, x, i;

char array[20][20];

printf ("Please enter N:");

cin >> N;

for (x=1;x<=20;x++) {

for (i=1;i<=20;i++) {

array[x] = '0';

}

}

printTable(array,N);

printf ("Please press any key to exit\n");

getch();

return 0;

}

the output that i must have is this one

table.jpg

and i have this one as output

outputl.jpg

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

First of all apart from cin its not very c++ like, use cout instead of printf and you wont have to do stuff like printf("%d", blah).

The second thing is that in c++ arrays are zero indexed so your for loops should look like:

for (int x = 0; x &lt; 20; ++x)

Finally, what would be even better than arrays would be to use a container (vector of vectors?) so you have access to reverse_iterator.

Link to comment
Share on other sites

  • 0

i found the solution

for(x=1;x<=n;x++) {

for(i=n;i>0;i--){

//elexume an to i enai mikrotero kai iso apo to x kai

//an isxi tote ta polaplasiazume alios afinume keno

//otan teliosi to for tu i tote pame sto for tu x opu alazume grami

//kai prosthetume +1 mexri to x na gini iso me to n

if(i<=x)

printf(" %2d",x*i);

else

printf(" ");

}

printf("\n");

}

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.