how to print an array backwards - c++

The user enteres a number which is put in an array and then the array needs to be orinted backwadrds
int main()
{
int numbers[5];
int x;
for (int i = 0; i<5; i++)
{
cout << "Enter a number: ";
cin >> x;
numbers[x];
}
for (int i = 5; i>0 ; i--)
{
cout << numbers[i];
}
return 0;
}

You're very close. Hope this helps.
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int numbers[5];
/* Get size of array */
int size = sizeof(numbers)/sizeof(int);
int val;
for(int i = 0; i < size; i++) {
cout << "Enter a number: ";
cin >> val;
numbers[i] = val;
}
/* Start index at spot 4 and decrement until k hits 0 */
for(int k = size-1; k >= 0; k--) {
cout << numbers[k] << " ";
}
cout << endl;
return 0;
}

You are very close to your result but you did little mistakes, the following code is the correct solution of the code you have written.
int main()
{
int numbers[5];
int x;
for (int i = 0; i<5; i++)
{
cout << "Enter a number: ";
cin >> numbers[i];
}
for (int i = 4; i>=0; i--)
{
cout << numbers[i];
}
return 0;
}

#include<iostream>
using namespace std;
int main()
{
//get size of the array
int arr[1000], n;
cin >> n;
//receive the elements of the array
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
//swap the elements of indexes
//the condition is just at "i*2" be cause if we exceed these value we will start to return the elements to its original places
for (int i = 0; i*2< n; i++)
{
//variable x as a holder for the value of the index
int x = arr[i];
//index arr[n-1-i]: "-1" as the first index start with 0,"-i" to adjust the suitable index which have the value to be swaped
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = x;
}
//loop for printing the new elements
for(int i=0;i<n;i++)
{
cout<<arr[i];
}
return 0;
}

#include <iostream>
using namespace std;
int main() {
//print numbers in an array in reverse order
int myarray[1000];
cout << "enter size: " << endl;
int size;
cin >> size;
cout << "Enter numbers: " << endl;
for (int i = 0; i<size; i++)
{
cin >> myarray[i];
}
for (int i = size - 1; i >=0; i--)
{
cout << myarray[i];
}
return 0;
}
of course you can just delete the cout statements and modify to your liking

this one is more simple
#include<iostream>
using namespace std;
int main ()
{
int a[10], x, i;
cout << "enter the size of array" << endl;
cin >> x;
cout << "enter the element of array" << endl;
for (i = 0; i < x; i++)
{
cin >> a[i];
}
cout << "reverse of array" << endl;
for (i = x - 1; i >= 0; i--)
cout << a[i] << endl;
}

answer in c++. using only one array.
#include<iostream>
using namespace std ;
int main()
{
int array[1000] , count ;
cin >> count ;
for(int i = 0 ; i<count ; i++)
{
cin >> array[i] ;
}
for(int j = count-1 ; j>=0 ; j--)
{
cout << array[j] << endl;
}
return 0 ;
}

#include <iostream>
using namespace std;
int main ()
{
int array[10000];
int N;
cout<< " Enter total numbers ";
cin>>N;
cout << "Enter numbers:"<<endl;
for (int i = 0; i <N; ++i)
{
cin>>array[i];
}
for ( i = N-1; i>=0;i--)
{
cout<<array[i]<<endl;
}
return 0;
}

Related

Reversing the order of a for loop output

I am currently writing a task where the user will input a number and it'll output a number of "*" depending on the number. Eg if the user inputted a 5, the answer would be:
*
**
***
****
*****
This is my current code:
#include <iostream>
using namespace std;
int number;
char star = '*';
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
for (int i = 0; i < number; i++)
{
for (int j = i; j < number; j++)
{
cout << star;
}
cout << " " << endl;
}
return 0;
}
If the number 5 was inputted, this would output:
*****
****
***
**
*
How would I go about reversing the order so that it is ascending order rather than descending.
You would need to use the for loop in descending instead of ascending order.
Example:
cout << "Input a number between 1 and 10" << endl;
cin >> number;
for (int i = number -1; i >= 0; i--) {
for (int j = i; j < number; j++) {
cout << star;
}
cout << "*" << endl;
}
return 0;
}
You can do that by just reversing the order of the change of i:
#include <iostream>
using namespace std;
int number;
char star = '*';
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
//for (int i = 0; i < number; i++)
for (int i = number - 1; i >= 0; i--)
{
for (int j = i; j < number; j++)
{
cout << star;
}
cout << "*" << endl;
}
return 0;
}
(Just how to reverse the order of the change of i is shown. The output of this program is not as expected.)
My preference is using i as the number of stars to print and avoiding using gloval variables and using namespace std;:
#include <iostream>
int main()
{
int number;
char star = '*';
std::cout << "Input a number between 1 and 10" << std::endl;
std::cin >> number;
for (int i = 1; i <= number; i++)
{
for (int j = 0; j < i; j++)
{
std::cout << star;
}
std::cout << std::endl;
}
return 0;
}
Just go from number all the way down instead of up:
for (int i = number; i > 0; i--)
{
for (int j = i; j > 0; j--)
{
cout << star;
}
std::cout << '\n';
}
Instead of j<number use j<i and instead of j=i use j=0:
#include <iostream>
using namespace std;
int number;
char star = '*';
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
for (int i = 0; i < number; i++)
{
for (int j = 0; j < i; j++)
{
cout << star;
}
cout << "*" << endl;
}
return 0;
}
there are multiple ways to achieve what you want. You can insert everything inside and array and print the array backwards, insert everything inside an array (starting from last position) and then print it in natural order, you can do it only by using indexes (like i and a lot of other persons did here). I also added a little input check, and put the code in functions for clearness:
#include <iostream>
using namespace std;
int number;
char star = '*';
void version1();
void version2();
int main()
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
while(number < 1 || number > 10)
{
cout << "I SAID BETWEEN 1 AND 10, IS EASY, RIGHT?" << endl;
cin >> number;
}
version1();
version2();
return 0;
}
void version1()
{
cout <<"Version 1: "<<endl;
for (int i = 0; i < number; i++)
{
for (int j = i; j < number; j++)
{
cout << star;
}
cout << endl;
}
}
void version2()
{
cout << "Version 2: (only by using indexes)" << endl;
int cap_index = 0;
for (int i = 0; i < number; i++)
{
cap_index = i + 1;
for (int j = 0; j < cap_index; j++)
{
cout << star;
}
cout << endl;
}
}
btw this is a more serious (and simple) way to check input:
do
{
cout << "Input a number between 1 and 10" << endl;
cin >> number;
}while(number < 1 || number > 10);
In my case i put it this way:
starting from 0 print '*' up to i + 1 times, this way you have an ascending order print:
first time (from 0 to 1) print '*' -> print once
second time (from 0 to 2) print '*' -> prints twice
and so on up to the inserted number
Obviously this isn't not the best way to do this nor the most elegant, like in nearly every situation there are a lot of possibilities.
ps. i'm not sure, but you have an error in your code, as it prints a '*' more, each time, i don't know how is your output like that
By using a std::string, you can avoid the need of an explicit inner loop.
#include <iostream>
#include <string>
int main()
{
int number;
char star = '*';
std::cout << "Input a number between 1 and 10" << "\n";
std::cin >> number;
for (int i = 0; i < number; i++)
{
std::cout << std::string (i+1, star) << "\n";
}
return 0;
}
Changing the second 'for' loop condition, in your function --
From,
for (int j = i; j < number; j++)
Change it to ,
for (int j = 0; j <= i; j++)

Equivalence Relation

I am stuck on what to do next... The program is suppose to check to see if entered Zero-One Matrix is an Equivalence relation (transitive, symmetric, and reflexive) or not. I am still new to C++ (started this semester). I know how to create the matrix using vector but not on how to check if it is equivalence relation or not..
I assume I need to use boolean function but I'm stuck on what I need to put in as an argument or if this is correct. My original thought was... so for symmetric it will look like (which I know this goes after #include and beofre int main(). Any help would be awesome.
bool isSymmetric(vector<int> &vect, int Value)
{
for (int i = 0; i < Value; i++)
for (int j = 0; j < Value; j++)
if (vect[i][j] != vect[j][i])
return false;
return true;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< vector<int> > vec;
cout << "NxN matrix N: ";
int Value;
cin >> Value;
cout << Value << "x" << Value << " matrix\n";
for (int i = 0; i < Value; i++) {
vector<int> row;
for (int j = 0; j < Value; j++) {
cout << "Enter a number (0 or 1): ";
int User_num;
cin >> User_num;
while (User_num != 0 && User_num != 1) {
cout << "Invalid Entry! Enter 0 or 1!\n";
cout << "Enter a number (0 or 1): ";
cin >> User_num;
}
row.push_back(User_num);
}
vec.push_back(row);
}
cout << endl;
for (int i = 0; i < Value; i++) {
for (int j = 0; j < Value; j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}

after inputting the values how can i display the values under one bracket e.g "{6,7,8,9}

#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
}
return 0;
}
You can use std::cout like :
#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
if(i ==0)
std::cout<<"{" <<array[i];
else if(i == n-1)
std::cout<<","<<array[i]<<"}";
else
std::cout<<","<<array[i];
}
return 0;
}
#mystic's answer uses arrays, which works fine. You can also use vector. There are more advanced methods of iterating over a vector, but I have not included that here to keep it simple.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> intVector{};
int n;
int input;
cout << "No. of values : ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input;
intVector.push_back(input);
}
// Print out the array
cout << "{";
for(int i = 0; i < intVector.size(); i++) {
cout << intVector[i];
// print out the comma, except for the last number
if(i < intVector.size() - 1) {
cout << ", ";
}
}
cout << "}" << endl;
return 0;
}
If you want to use an iterator for printing the array, you can replace the print loop with this code:
// Print out the array
cout << "{";
for(auto i=intVector.begin(); i!=intVector.end(); ++i) {
if (i != intVector.begin()) {
cout << ", ";
}
cout << *i;
}
cout << "}" << endl;

Diamond shape inner for with an output number 1 to 9 C++

so i got this code.
int n;
int m=1;
int a=9;
cout<<"Enter N Number = ";
cin>>n;
n=n*2-1;
for(int y=1;y<=n;y++)
{
for(int x=1;x<=n;x++)
{
if(x==y+n/2 || x==y-n/2 || x==n-y+1-n/2 || x==n-y+1+n/2)
{
if(m<=9)
{
cout<<m++;
}
else if(m>9&&a>0)
{
a--;
cout<<a;
}
}
else
{
cout<<" ";
}
}
cout<<endl;
}
This is what i get :
Diamond Shape(Fail)
And what i expected is there's no number "0" on the bottom of the shape, so after it printed the number 1 its bounce back to number 2,3 and so on
pardon for my bad english
Do you want it to be like this?
Check this out!
I can code in Pascal, C++ and Java.
PS: I am not a native English speaker too.
First Answer: This is not the best answer, I will improve it tomorrow afternoon.
#include <iostream>
using namespace std;
int get() {
static int counter = 0;
static bool reverse = false;
!reverse ? counter++ : counter--;
if (counter==10 or counter==0) reverse = !reverse;
counter==10 ? counter = 8 : 0;
counter==0 ? counter = 2 : 0;
return counter;
}
int main() {
int number;
cout << "Enter N Number = ";
cin >> number;
//forward:
for (int i = 0; i < number; i++) {
for (int j = number-i-1; j > 0; j--)
cout << " ";
cout << get();
if (i!=0) {
for (int j = 0; j < i * 2 -1; j++) cout << " ";
cout << get();
}
cout << endl;
}
//backward:
for (int i = number-1; i > 0 ; i--) {
for (int j = number-i; j > 0; j--)
cout << " ";
cout << get();
if (i!=1) {
for (int j = i * 2 -3; j > 0; j--) cout << " ";
cout << get();
}
cout << endl;
}
return 0;
}
Second Answer: Improved loops
#include <iostream>
using namespace std;
int nextNumber() {
static int counter = 0;
static bool reverse = true;
!reverse ? counter++ : counter--;
if (counter==-1) counter=1;
if (counter==9 or counter==1) reverse = !reverse;
return counter;
}
int main() {
int number;
cout << "Enter N Number = ";
cin >> number;
int space = number;
int middle = -3;
for (int row = 0; row < number*2-1; row++) {
if(row<number) {
space--;
middle += 2;
}
else {
space++;
middle -= 2;
}
for (int i = 0; i < space; i++)
cout << " ";
cout << nextNumber();
if(row!=0 and row!=number*2-2) {
for (int i = 0; i < middle; i++)
cout << " ";
cout << nextNumber();
}
cout << endl;
}
return 0;
}

Printing Elements of 3D Array of pointers

Here is my full code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
int ***my3DArray;
int length, width, height;
bool doAgain = true;
string answer("");
string yes("y");
do{
cout << "Enter length: ";
cin >> length;
cout << "\nEnter width: ";
cin >> width;
cout << "\nEnter height: ";
cin >> height;
srand((unsigned)time(NULL));
my3DArray = new int**[length];
for (int i= 0; i < length; ++i){
my3DArray[i] = new int*[width];
for(int j = 0; j< width; ++j){
my3DArray[i][j] = new int[height];
for(int k = 0; k < height; ++k){
my3DArray[i][j][k] = rand()%100;
cout << my3DArray[i][j][k] << " ";
do{
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: ";
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
}
cout << endl;
}
cout << endl;
}
for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
delete[]my3DArray[i][j];
}
delete[]my3DArray[i];
}
delete[]my3DArray;
my3DArray = NULL;
cout << "Again? (y, n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
}
This prints a 3D array of integers, I need to write a function called tick that returns the element of user specified coordinates i j k. That part of the code is this
do{
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: ";
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
when I have this in my code, it doesnt print out the 3D array and I get a segmentation fault when it's supposed to print the element. Any Ideas? Thanks in advance
instead of int ***my3DArray use int my3DArray[256][256] when declaring an array is just a pointer to the first item in the list. you then run a for loop to go through that list.