I want to create an array of Bitset .Binary Bitset(example "100","1010",etc)
After that I want to input from user and store in the the Bitset .
I have tried the following line but it says error.
#include<bits/stdc++>
using namespace std;
int main()
{
int n,i;
string bit_string;
cin>>n // size of Bitset array.
bitset<8> brr[n];//
for(i=0;i<n;i++)
{
cin>>bit_string;
brr[i](bit_string);
}
return 0;
}
I want to create n Bitset each of size 8 bits.Where n is given by user.
my input is binary string like.
"110010","001110"
please help
The error ocurrs because you are trying to creat a C-style array using n which is not compile-time constant. It's not possible to creat a C-style array without being n known at compile time.
The following is a good way to do what you want
Creat a std::vector<std::bitset<8>> to hold your bitset<8>s, as follows.
Note that the code ignores the excess of characters in strings iput like "111111110" (makes it "11111111") and treats any character except '1' as if it were '0' and if the input string is less than 8 characters, the code adds zeros by the default of the bitsets
#include <vector>
#include <bitset>
#include <iostream>
int main() {
int n, i;
std::string bit_string;
std::cout << "Enter the size";
std::cin >> n; // size of Bitset array.
std::vector<std::bitset<8>> brr(n);//
for (i = 0; i < n; i++) {
std::cin >> bit_string;
for (int j{}; j < bit_string.size() && j < 8; ++j) {
brr[i][j] = (bit_string[j] == '1') ? 1 : 0;
}
}
//To test
for(auto const& el :brr)
{
for(int i{}; i < 8;)
std::cout << el[i++];
std::cout<<"\n";
}
}
See Why is "using namespace std;" considered bad practice?
and
Why should I not #include <bits/stdc++.h>?
For dynamic count of the objects , Please try vector<> instead of array[]
#include<bits/stdc++>
using namespace std;
int main()
{
int n, i;
string bit_string;
cin >> n; // size of Bitset array.
vector<bitset<8>> arr; //size()=>0
arr.resize(n); //size()=>n
for (i = 0; i < n; i++)
{
cin >> bit_string;
bitset<8>& br = arr[i]; //get the i of n
int maxlen = 8;
if (bit_string.size() <= 8)
maxlen = bit_string.size();
else
cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;
for (int j = 0; j < maxlen; j++)
{
if (bit_string[j] == '1')
br.set(j, true);
}
//cout << endl << br << endl; //output test
}
return 0;
}
If you still want to use array , please try this way
#include<bits/stdc++>
using namespace std;
int main()
{
int n, i;
string bit_string;
cin >> n; // size of Bitset array.
bitset<8>* arr = new bitset<8>[n];
for (i = 0; i < n; i++)
{
cin >> bit_string;
bitset<8>& br = arr[i]; //get the i of n
int maxlen = 8;
if (bit_string.size() <= 8)
maxlen = bit_string.size();
else
cout << "warning invalid len " << bit_string.size() << " of " << bit_string << endl;
for (int j = 0; j < maxlen; j++)
{
if (bit_string[j] == '1')
br.set(j, true);
}
//cout << endl << br << endl; //output test
}
delete[] arr; //IMPROTAND , delete the array and free memory
return 0;
}
Related
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
const int SZ = 5;
string word[SZ];
int len,min,large,max;
for(int i = 0; i < SZ; i++){
cout << "Please Enter a word five times: ";
cin >> word[i];
}
min = word[SZ].length();
for(int i = 0; i < SZ; i++){
if(word[i] < len){ // I'm getting it on this line of code
min = word[i];
}
}
return 0;
}
My code is not properly running and currently I'm trying to find the smallest word of 5 different words inputted by the user.
There are some mitakes in your code:
Your first cout should be above the for loop rather than inside of it.
word[SZ].length() is accessing a string that is out of bounds of the array. You should be accessing the 1st string at word[0] instead.
on the statement if(word[i] < len), len is uninitialized. For that matter, you don't even need len at all, use min instead. But more importantly, you are comparing a string to an int, which is why you are getting an error. You need to compare the string's length() value instead.
Try this:
#include <iostream>
#include <string>
using namespace std;
int main () {
const int SZ = 5;
string word[SZ];
int min, max;
cout << "Please enter " << SZ << " words: ";
for(int i = 0; i < SZ; i++){
cin >> word[i];
}
min = max = word[0].length();
for(int i = 1; i < SZ; i++){
if (word[i].length() < min){
min = word[i].length();
}
if (word[i].length() > max){
max = word[i].length();
}
}
cout << "min: " << min << ", max: " << max << endl;
return 0;
}
Online Demo
For starters, this (modified) statement:
cout << "Please Enter " << SZ << " words: ";
should be placed before the for loop:
for(int i = 0; i < SZ; i++){
cin >> word[i];
}
In this statement:
min = word[SZ].length();
you are using a non-existent element of the array with the index SZ, while the valid range of indices for the array is [0, SZ).
So, rewrite it like:
auto min = word[0].length();
Within the for loop, this statement:
if(word[i] < len)
does not make sense. You need to write:
if(word[i].length() < min)
Also, this statement:
min = word[i];
has invalid operands for the assignment operator.
The loop can look like
auto min = word[0].length();
for( size_t i = 1; i < SZ; i++ ){
if( word[i].length() < min ){
min = word[i].length();
}
}
If you need to find the word with the minimal length then the for loop can look the following way
size_t min = 0;
for( size_t i = 1; i < SZ; i++ ){
if( word[i].length() < word[min].length() ){
min = i;
}
}
Pay attention to that, there is a standard algorithm std::min_element declared in the header <algorithm> that can be used instead of the manually written loop.
The code runs fine in my compiler but shows runtime error on codeforces. I found out that I should avoid unitialized variables but I think I've already covered that.
#include <iostream>
using namespace std;
int main()
{
int i, n, flag = 1, k = 1;
int hours[n];
int minutes[n];
cin >> n;
for (i = 0; i < n; i++)
{
cin >> hours[i];
cin >> minutes[i];
}
for (i = 0; i < n; i++)
{
if (hours[i] == hours[i + 1] && minutes[i] == minutes[i + 1])
{
flag++;
if (flag > k)
k = flag;
}
else
flag = 1;
}
cout << k;
return 0;
}
The problem lies here
int i, n, flag = 1, k = 1;
int hours[n];
int minutes[n];
You declare variables n, but you don't initialize it, and then you use it to specify an array size. Uninitialized variables of type int contain garbage data, and using it is undefined behavior, whatever happens is unspecified, and you can not rely on it.
If you must create an array of size specified by a user input, you need to allocate at runtime.
int *hours;
int *minutes;
cin >> n;
hours = new int[n];
minutes = new int[n];
// Rest of the code
// Remember to delete them
delete[] minutes;
delete[] hours;
Better yet, use a container provided by the standard library, a std::vector would be a perfect container to use here
#include <vector>
std::vector<int> hours;
std::vector<int> minutes;
cin >> n
hours.resize(n);
minutes.resize(n);
VLAs are not standard C++ but are allowed as an extension. The first problem in your code is that you use the variable n without initialization. This causes undefined behavior according to the standard.
You need to move the call to cin() to above the arrays that use n at declaration. The second problem in your code is that the expression inside the if statement attempts to access an element outside the bounds of your array. This also causes undefined behavior.
Here is a refined version of your code:
#include <iostream>
using namespace std;
int main()
{
int n;
int k = 1;
int flag = 1;
cout << "Enter a value for n:" << endl;
cin >> n;
int *hours = new int[n];
int *minutes = new int[n];
cout << "Enter values for hours:" << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << ":";
cin >> hours[i];
}
cout << "Enter values for minutes:" << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << ":";
cin >> minutes[i];
}
// note here: 'i' goes to (n - 1) instead of (n) so as not to be out of range
for (int i = 0; i < n - 1; i++)
{
if ((hours[i] == hours[i + 1]) && (minutes[i] == minutes[i + 1]))
{
flag++;
if (flag > k)
{
k = flag;
}
}
else
{
flag = 1;
}
}
cout << "The value of k is: " << k << endl;
delete hours;
delete minutes;
return 0;
}
Hi i'm the beginner of c++.
This time, I try to reverse Array 's element-order using dynamic Allocation Operator.
For example an array{1,2,3,4} will be rearranged {4,3,2,1} through calling function 'reverseArray'.
Everything works fine but somehow i got unwanted integer '-1' along with rearranged Array.
For example {-1,4,3,2,1}.
It means i did wrong and i really want to learn my fault.
Here is my code, please help me to figure out.
#include<iostream>
#include<new>
using namespace std;
void reverseArray(int [] , int);
int main(){
int num=0;
cout << "enter size of pointer : " ;
cin >> num ;
int *pointerArray = new int [num];
cout << "enter integer numbers in pointer" << endl;
for(int index = 0 ; index < num ; index++){
cin >> pointerArray[index];
}
reverseArray(pointerArray, num);
delete[] pointerArray;
return 0 ;
}
void reverseArray(int reverse[], int Size){
int*ptArray[Size];
cout << "the reverse order of entered numbers is : " << endl;
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
return ;
}
This function
void reverseArray(int reverse[], int Size){
int*ptArray[Size];
cout << "the reverse order of entered numbers is : " << endl;
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
return ;
}
does not make sense.
For starters variable length arrays
int*ptArray[Size];
is not a standard C++ feature.
Secondly in this loop
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
there is access beyond the arrays.
For example let's assume that Size is equal to 1.
In this case within the ,loop we have
ptArray[0] = &reverse[1];
and then
ptArray[1] = &reverse[0];
However the only valid index for such arrays is 0.
It is unclear what you are trying to do.
If you want to reverse an array in place then the function can look like
void reverseArray( int a[], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
// you can use the standard function std::swap here
int tmp = a[i];
a[i] = a[Size - i - 1];
a[Size - i - 1] = tmp;
}
}
And in main after calling the function you can output the reversed array .
Pay attention to that there is standard algorithm std::reverse that can do the rask.
You could just write
std::reverse( reverse, reverse + num );
Here is a demonstrative program.
#include <iostream>
#include <algorithm>
void reverseArray( int a[], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
// you can use the standard function std::swap here
int tmp = a[i];
a[i] = a[Size - i - 1];
a[Size - i - 1] = tmp;
}
}
int main()
{
const size_t N = 5;
int *a = new int[5] { 1, 2, 3, 4, 5 };
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
reverseArray( a, N );
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
std::reverse( a, a + N );
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
delete [] a;
return 0;
}
Its output is
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
In your code you access the array out-of-bounds on the very first iteration, because for an array of size Size valid indices are 0 up to Size-1
It is not clear why you create an array of pointers, and int*ptArray[Size]; is a variable lenght array (VLA) which is not standard C++. Further, you do not need to include <new>.
To print a dynamically sized (ie size is taken from input) array you would use a std::vector and a loop:
#include <vector>
#include <iostream>
int main(){
size_t size;
std::cout << "enter size: ";
std::cin >> size;
std::vector<int> data(size);
for (auto& element : data) std::cin >> element;
for (size_t i = 0; i < data.size(); ++i) {
std::cout << data[ data.size()-1-i ]; // first element printed is data[data.size()-1]
}
}
If you want to reverse the array, not just print it in reverse order, there is std::reverse in <algorithm>. The algorithm also works with dynamically allocated arrays:
#include <iostream>
#include <algorithm>
int main(){
size_t size;
std::cout << "enter size: ";
std::cin >> size;
int* data = new int[size];
for (size_t i=0; i<size; ++i) std::cin >> data[i];
std::reverse(data,data+size);
for (size_t i=0; i<size; ++i) std::cout << data[i];
delete [] data;
}
#include
using namespace std;
int main() {
int n;
cin>>n;
int temp=n;
int rem=0;
int i=0;
while (n>0) {
n= n/10;
i++;
}
int *arr = new int(i);
i=0;
while (temp>0) {
rem=temp%10;
arr[i]=rem;
i++;
temp= temp/10;
}
int t=0;
while(t<i) {
cout<<arr[t]<<" ";
t++;
}
return 0;
}
Beginner programmer. I have an assignment where I am to output numbers using loops. The input is the size of the output. The first assignment is to output the same #s vertically: For example. Input? 5. Output:
12345
12345
12345
12345
12345
My current code is incorrect but here is what I have:
int main(){
unsigned size;
cout <<"Size: ? ";
cin >>size;
cout <<"Numbers Vertically!" <<endl;
for ( unsigned r = 0; r < size; r++ ){
for ( unsigned c = 0; c < size; c++)
cout <<size;
cout <<endl;
}
cout <<endl;
}
You need to make two changes.
First is to actually read in the input using cin.
Second is to print c+1 in your inner loop instead of size.
Here is the code:
int main() {
unsigned size;
cout <<"Size: ? ";
cin >> size; // Read input size
cout <<"Numbers Vertically!" <<endl;
for ( unsigned r = 0; r < size; r++ ) {
for ( unsigned c = 0; c < size; c++) {
cout << c+1; // Print c+1 instead of size
}
cout <<endl;
}
cout <<endl;
}
Here is a running example
You're missing a 'cin >> size;' instruction.
I prefer this way.
#include <iostream>
using namespace std;
int main(){
unsigned size = 10;
unsigned n = size * size;
unsigned i = 1;
for (unsigned r = 0; r < n; r++ ){
cout << i % (size + 1);
if (i % (size + 1) == size) {
cout <<endl;
i = 1;
}
else {
i++;
}
}
cout <<endl;
return 0;
}
Sorry, I'm a brand new newbie to C++ and programming and I'm getting a heap corruption error. I think Im writing in unallocated memory but I can't seem to find where the error is... the program is soppuse to take user input values and rearrange them so that they would ascend. I'm also learning templates too.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
template <typename T>
void sort(T arrayz[], int size, char ch)
{
T temporary;
int k, j;
if (ch = 'a')
{
for (k = 0; k < size; k++)
{
for (j = 0; j < size; j++)
{
temporary = arrayz[j];
arrayz[j] = arrayz[j + 1];
arrayz[j + 1] = temporary;
}
}
}
}
int main()
{
int choices, range, i;
int x;
char ch;
cout << ("Enter the amount of numbers you want =>");
cin >> x;
int *numbers = new int[x];
if (!numbers)
{
cout << "Memory Allocation error!";
cin.get();
exit(1);
}
for (int i = 0; i<x; i++)
{
cout << "Option number" << i + 1 << " =>";
cin >> numbers[i];
}
cout << "Do you want ascending or descending values (a/d) =>" ;
cin >> ch;
if (ch = 'a')
{
sort(numbers, x, ch);
}
else if (ch = 'd')
{
sort(numbers, x, ch);
}
delete[] numbers;
fflush(stdin);
cin.get();
return 0;
}
In your sort function, you are accessing elements at index j + 1. However, this is out of bounds. The valid indexes for your arrayz array are 0 through size-1. When j is size-1, j+1 is size, which accesses past the end of the array.