Print out array values in pairs
Oct 9, 2020 at 5:37pm UTC
Hello everyone.Proabably it's quite simple, but i'm kind a stuck with one thing my task is to make queue with pop and push functions and also have to display the queue/array, i have a default constructor with parameter which passes the arrays size. So the thing is i need to print out the array values by two which are inside brackets and they are separeted by comma, when the bracket closes it separetes another pair with semicolon.It would supposed to look like {1,2};{3,4};{5,6}.I would be very greatfull for every help.Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#include <iostream>
#include <cstdlib>
using namespace std;
class FIFO
{
private :
int front;
int rear;
int *arr;
int SIZE;
public :
FIFO(int capacity)
{
front = -1;
rear = -1;
SIZE = capacity;
for (int i = 0; i < SIZE; i++)
{
arr[i] = 0;
}
}
bool isEmpty()
{
if (front == -1 && rear == -1)
return true ;
else
return false ;
}
bool isFull()
{
if (rear == SIZE-1)
return true ;
else
return false ;
}
void Push(int val)
{
if (isFull())
{
cout<<"Queue is full" <<endl;
return ;
}
else if (isEmpty())
{
rear = front = 0;
arr[rear] = val;
}
else
{
rear++;
arr[rear] = val;
}
}
int Pop()
{
int x;
if (isEmpty())
{
cout<<"Queue is empty " <<endl;
return 0;
}
else if (front == rear)
{
x = arr[front];
arr[front] = 0;
rear = -1;
front = -1;
return x;
}
else
{
x = arr[front];
arr[front] = 0;
front++;
return x;
}
}
void Display()
{
cout<<"All values in the queue are - " <<endl;
for (int i = 0; i < SIZE; ++i)
{
cout<<"{" <<arr[i]<<",};" ;
}
}
};
int main()
{
FIFO f1(6);
int value;
char option;
do
{
cout<<"What operation do you want to perform? Select Option number." <<endl;
cout<<"+. Push()" <<endl;
cout<<"-. Pop()" <<endl;
cout<<"*. Display()" <<endl;
cout<<"$. Exit" <<endl<<endl;
cin>>option;
switch (option)
{
case '+' :
cout<<"Push operation \nEnter an item to push in the queue " <<endl;
while (!(cin >> value))//
{
cout << "ERROR: a number must be entered: " ;
cin.clear();
cin.ignore(123, '\n' );
}
f1.Push(value);
break ;
case '-' :
cout<<"Pop Operation \nPoped value: " <<f1.Pop()<<endl;
break ;
case '*' :
cout<<"Display function called - " <<endl;
f1.Display();
break ;
case '$' :
break ;
default :
cout<<"Enter proper option number " <<endl;
}
}while (option != '$' );
return 0;
}
Oct 9, 2020 at 7:50pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include<iostream>
using namespace std;
int main()
{
const int size=6;
int ar[size] = {1,2,4,8,16,32};
if (size%2==0)
{
for (int i=0; i<size; i++)
{
if (i%2==0)
{
cout << "{" << ar[i] << "," ;
}
else
{
cout << ar[i] << "};" ;
}
}
}
}
output:
Oct 10, 2020 at 1:02am UTC
Another way:
1 2 3 4 5 6 7 8 9 10
#include<iostream>
int main()
{
const int size=6;
int ar[size] = {1,2,4,8,16,32};
for (int i=0; i<size; i+= 2 )
std::cout << "{" << ar[i] << "," << ar[i+1] << "};" ;
}
Oct 10, 2020 at 8:34am UTC
Thank you guys.
Oct 10, 2020 at 10:23am UTC
1 2 3 4 5 6 7 8 9 10
FIFO(int capacity)
{
front = -1;
rear = -1;
SIZE = capacity;
for (int i = 0; i < SIZE; i++)
{
arr[i] = 0;
}
}
The constructor has a major problem - you're not allocating any memory for arr! Also, you need a destructor to free the allocated memory.
1 2
FIFO(int capacity) : front(-1), rear(-1), SIZE(capacity), arr(new int [capacity] {}) {}
~FIFO() {delete [] arr; }
Also, these can be simplified:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bool isEmpty()
{
if (front == -1 && rear == -1)
return true ;
else
return false ;
}
bool isFull()
{
if (rear == SIZE-1)
return true ;
else
return false ;
}
becomes:
1 2
bool isEmpty() {return front == -1 && rear == -1; }
bool isFull() {return rear == SIZE - 1; }
PS Shouldn't Display() just display the current values in the queue - and not the whole queue?
Last edited on Oct 10, 2020 at 10:28am UTC
Oct 10, 2020 at 5:19pm UTC
againtry's code...1 2 3 4 5 6 7 8 9 10
#include<iostream>
int main()
{
const int size=6;
int ar[size] = {1,2,4,8,16,32};
for (int i=0; i<size; i+= 2 )
std::cout << "{" << ar[i] << "," << ar[i+1] << "};" ;
}
...will try to print from out of allowed memory by
ar[i+1]
if
size=
odd number.
Topic archived. No new replies allowed.