so I am having a little bit of trouble getting this program to work without it throwing the errors:
"request for member 'insertArray' in 'arr', which is of non-class type 'int [10]' " and
"request for member 'print' in 'arr', which is of non-class type 'int [10]' ".
Both of those errors have to do with using my functions with an array, you can see the code below:
#include <iostream>
using namespace std;
int size = 0;
int *arr;
void insertArray(int val)
{
int i = 0;
while (i < size && arr[i] < val)
i++;
for (int k = size - 1; k >= i; k--)
{
arr[k + 1] = arr[k];
}
arr[i] = val;
size++;
}
void print()
{
cout << "The array is: ";
for (int j = 0; j < size; j++)
{
cout << arr[j];
}
}
int main()
{
int arr[10];
cout << "Please enter 5 values: \n";
for (int i = 0; i < 5; i++)
{
int num = 0;
cin >> num;
arr.insertArray(num); // Error 1: vs code error squiggles say: "expression must have class type"
arr.print(); // Error 2: vs code error squiggles say: "expression must have class type"
}
return 0;
}
I don't know how to fix the errors or what they mean either.
Thanks for the help in advance folks!
arr is just a plain-old C array, you can't define new functions on it. You need to define a class or struct if you want to do that.
What you have here is procedural code, so you're constrained by that model and must pass in arr as an argument.
I've tried to wrangle your original code into this form with as few modifications as necessary:
#include <iostream>
void printArr(const int* arr, const size_t size)
{
std::cout << "The array is: ";
for (int j = 0; j < size; j++) {
std::cout << arr[j];
}
std::cout << std::endl;
}
int main()
{
const size_t count = 5;
int arr[count];
std::cout << "Please enter 5 values: \n";
for (int i = 0; i < 5; i++)
{
std::cin >> arr[i];
}
printArr(arr, count);
return 0;
}
The whole insertArray function was just too confusing so I deleted it presuming what you were trying to do was add things at the end of the array anyway.
Related
I have some code written but I'm not sure why the reversed array is not giving me the exact values I need. I created a second array the same size as the first and used nested for loops to fill the second with the contents of the first in reverse.
See below:
#include <iostream>
using namespace std;
int main()
{
// Ask for how big the array is
int n;
cout << "how big is the array?" << endl;
cin >> n;
// create array
int a[n];
// create second array
int b[n];
// ask for contents of the 1st array
cout << "what's in the array?" << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
// reverse the array
for (int i = n - 1; i >= 0; i--)
{
for (int k = 0; k < n; k++)
{
b[k] = a[i];
break;
}
}
// print out the new array
for (int k = 0; k < n; k++)
{
cout << b[k] << endl;
}
return 0;
}
you don't need 2 bucles for fill the second array
try with:
//reverse the array
s = 0;
for (int i=n-1;i>=0;i--){
b[n]=a[s];
s++;
}
Try something like this:
#include <algorithm>
#include <iostream>
#include <vector>
namespace {
template <typename IStream>
[[nodiscard]] int readOneIntFrom(IStream& istream) {
int x;
istream >> x;
return x;
}
}
int main()
{
// Ask for how big the array is
std::cout << "how big is the array?" << std::endl;
auto n = readOneIntFrom(std::cin);
// create array
std::vector<int> a;
// ask for contents of the 1st array
std::cout << "what's in the array?" << std::endl;
for (int i = 0; i < n; i++)
{
a.emplace_back(readOneIntFrom(std::cin)); // Make a new entry at the end of a.
}
// Construct b from a backward. (Or do auto b = a; std::reverse(b.begin(), b.end());
auto b = std::vector<int>(a.rbegin(), a.rend());
// print out the new array
for (const auto& bi : b)
{
std::cout << bi << std::endl;
}
return 0;
}
I'm trying to refresh myself in C++ before my college starts again and I ran into some problems. I'm using a bubblesort function given by my professor and I'm struggling to run it in my int main. The function parameters is bubblesort(int *a, int length), so I used bubblesort(a, sizeof(a)/sizeof(*a) ).
The compiler shows an error ' a ' is declared void. I tried searching up for an answer if I made a mistake but I couldn't catch my error. If you understand why I am getting this error can you please explain in detail what I'm missing or doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void bubblesort(int *a, int length)
{
int i, temp, finished = 0;
while(!finished)
{
finished = 1;
for( i = 0; i< length-1; i++)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
finished = 0;
}
}
}
}
int main()
{
int a[] = {5,1,7,9,4,3};
for (int i = 0;i < sizeof(a)/sizeof(*a);i++){ cout << a[i]; }
cout << endl;
void bubblesort(a, sizeof(a)/sizeof(*a));
for (int i = 0;i < sizeof(a)/sizeof(*a);i++){ cout << a[i]; }
}
You don't need return type in function call.
Remove void from the line
void bubblesort(a, sizeof(a)/sizeof(*a));
and make it
bubblesort(a, sizeof(a)/sizeof(*a));
You can't specify the return type of a function when making a function call, so you need:
bubblesort(a, sizeof(a)/sizeof(*a)); // no void at the beginning
However, you have a fixed-size array, so the call could also simply be:
bubblesort(a, std::size(a));
Also, this loop:
for (int i = 0;i < sizeof(a)/sizeof(*a);i++){ cout << a[i]; }
can be rewritten like this:
for (int elem : a)
cout << elem;
My code is supposed to print the Union and Intersection of two sets of integers.
Why do I get this warning?
Is it because I use dynamic arrays and it's size could be anything in runtime?
How can I fix it? My code works fine but this warning really bugs me.
P.S: I know it would be a lot easier to use std::vector but my teacher required to use arrays.
#include <iostream>
using namespace std;
void UnionFunc(int[],int,int[],int,int[],int&);
void IntersectionFunc(int[], int, int[], int, int[], int&);
int main() {
int* A;
int SizeA;
int* B;
int SizeB;
int* Union;
int UnionSize=0;
int* Intersection;
int IntersectionSize=0;
cout << "Enter the Size of First Set : "; cin >> SizeA;
A = new int[SizeA];
cout << "Enter the Size of Second Set : "; cin >> SizeB;
B = new int[SizeB];
Intersection = new int[SizeA >= SizeB ? SizeB : SizeA];
Union = new int[SizeA + SizeB];
for (int i = 0; i < SizeA; i++) {
cout << "Set A[" << i + 1 << "] = ";
cin >> A[i];
}
for (int i = 0; i < SizeB; i++) {
cout << "Set B[" << i + 1 << "] = ";
cin >> B[i];
}
UnionFunc(A,SizeA,B,SizeB,Union,UnionSize);
IntersectionFunc(A, SizeA, B, SizeB, Intersection, IntersectionSize);
cout <<endl<< "Union Set : ";
for (int i = 0; i < UnionSize; i++) {
cout << Union[i] << ",";
}
cout <<endl <<"Intersection Set : ";
for (int i = 0; i < IntersectionSize; i++) {
cout << Intersection[i] << ",";
}
system("pause>n");
return 0;
}
void UnionFunc(int A[],int SizeA, int B[],int SizeB, int Union[],int &UnionSize) {
//Adding First Array to Union Array
for (int i = 0; i < SizeA;i++) {
Union[i] = A[i];
UnionSize++;
}
//Checking if second array's elemnts already exist in union arry, if not adding them
bool exist;
for (int i = 0; i < SizeB; i++) {
exist = false;
for (int j = 0; j < UnionSize; j++) {
if (B[i] == Union[j] ) {
exist = true;
}
}
if (exist == false) {
Union[UnionSize] = B[i];
UnionSize++;
}
}
}
void IntersectionFunc(int A[], int SizeA, int B[], int SizeB, int Intersection[], int& IntersectionSize) {
for (int i = 0; i < SizeA; i++) {
for (int j = 0; j < SizeB; j++) {
if (A[i] == B[j]) {
Intersection[IntersectionSize] = A[i];
IntersectionSize++;
}
}
}
}
Is it because I use dynamic arrays and it's size could be anything in
runtime?
Yes! The compiler doesn't know (and, as your code is written, can't know) that both SizeA and SizeB will be 'valid' numbers - so the size of the three int arrays you create could be less than is required for the Intersection[i] 'read' to be valid.
A 'quick and dirty' fix for this is to provide a visible guarantee to the compiler that the arrays you create will be at least a certain size, like this:
A = new int[max(1,SizeA)]; // Compiler can now 'see' a minimum size
And similarly for the other allocations you make with the new[] operator.
(I have tested this with VS2019, adding the max(1,SizeA) and max(1,SizeB) 'fixes' to just the allocations of A and B and the warning is removed.)
In the following code, when I'm removing cout statement (line after //******)then it is causing a change in the value of "i".
I used TDM-GCC 4.9.2 32 bit release and TDM-GCC 5.1.0 compilers.
I ran this code on codechef and there it runs fine and cout statement is not affecting the value of "i".
#include<iostream>
using namespace std;
int subset(int [], int);
int main()
{
int size,i,ans;
cout<<"size of array : ";
cin>>size;
int arr[size];
for(i = 0 ; i<size;i++)
{
cin>>arr[i];
}
ans = subset(arr,size);
cout<<"ans = "<<ans;
return 0;
}
int subset(int arr[], int size)
{
int i,j, tsum=0, completed=0;
for(i = 0 ;i<size;i++)
tsum = tsum + arr[i];
int carr[tsum+1],temp;
for(i=0;i<size;i++)
{
temp = arr[i];
carr[temp] = 1;
for(j=i+1;j<size;j++)
{
temp = temp + arr[j];
carr[temp] = 1;
}
}
for(i=1;i<=tsum;i++)
{
if(carr[i]!=1)
{
//************************************
cout<<"i : "<<i<<endl;
break;
}
}
return i;
}
Sample input :
size of array : 3
1
2
5
sample output without cout statement :
ans = 6
sample output having cout statement :
i : 4
ans = 4
Actual answere is 4 for the input.
The main problem seems to be that carr is uninitialized.
It is declared as
int carr[tsum+1]
with no initializer.
Later on some elements are set, but always to 1:
carr[temp] = 1;
In the last loop carr is examined:
if(carr[i]!=1)
This condition makes no sense. Either carr[i] has been set, then it is guaranteed to be 1, or it is uninitialized, in which case this comparison has undefined behavior.
Note that variable-length arrays are not standard C++.
To solve the problems as stated by Some Programmer Dude and melpomene, i.e. Variable-length arrays are not standard C++ and carr is uninitialized. Use c++ vectors and initialize them correctly. That would look something like this:
#include <iostream>
#include <vector>
using namespace std;
int subset(const std::vector<int>, const int);
int main()
{
int size, i, ans;
cout << "size of array : ";
cin >> size;
std::vector<int> arr(size);
for (i = 0; i < size; i++)
{
cin >> arr[i];
}
ans = subset(arr, size);
cout << "ans = " << ans;
return 0;
}
int subset(const std::vector<int> arr, const int size)
{
int i, j, tsum = 0, completed = 0;
for (i = 0; i < size; i++)
tsum = tsum + arr[i];
std::vector<int> carr(tsum + 1, 0);
int temp;
for (i = 0; i < size; i++)
{
temp = arr[i];
carr[temp] = 1;
for (j = i + 1; j < size; j++)
{
temp = temp + arr[j];
carr[temp] = 1;
}
}
for (i = 1; i <= tsum; i++)
{
if (carr[i] != 1)
{
//************************************
cout << "i : " << i << endl;
break;
}
}
return i;
}
I looked at the other answers, but it didn't really help since 1) I don't understand it (especially if it's not exactly my problem) and 2) I noticed that the way my class is learning how to program is vastly different from what I've seen online. For example, we are using std. So I'm sorry for the repetitive question.
Here are my compiler errors:
main.cc: In function ‘int sum_up(std::array)’:
main.cc:21:19: error: expected primary-expression before ‘]’ token
sum+=arr[i,size_t];
^
main.cc:18:9: warning: unused variable ‘size’ [-Wunused-variable]
size_t size = arr.size(); //A size_t is just an unsigned int on this system
^
main.cc: In function ‘int sum_up(std::vector<int>)’:
main.cc:28:9: warning: unused variable ‘size’ [-Wunused-variable]
size_t size = vec.size();
^
Here's my code:
It was actually pre-made by our teacher; our main focus was the functions and how to sum up C style and C++ style arrays/vectors. I did my code exactly like my friend's but for some reason mine won't compile.
Please and thank you for all the help.
#include <iostream>
#include <cstdlib>
#include <array>
#include <vector>
using namespace std;
const size_t ARR_SIZE = 5;
int sum_up(int arr[], size_t size) { //Function to sum up a C-style array
int sum = 0;
for (size_t i = 0; i < ARR_SIZE; i++)
sum += arr[i];
return sum; //Stub
}
int sum_up(array<int, ARR_SIZE> arr) {
size_t size = arr.size(); //A size_t is just an unsigned in
//on this system
int sum = 0;
for (size_t i = 0; i < ARR_SIZE; i++)
sum += arr[i, size_t];
return sum; //Stub
}
int sum_up(vector<int> vec) {
size_t size = vec.size();
int sum = 0;
for (size_t i = 0; i < ARR_SIZE; i++)
sum += vec[i];
return sum; //Stub
}
void die() {
cout << "Invalid input.\n";
exit(EXIT_FAILURE);
}
int main() {
int c_arr[ARR_SIZE] = {}; //Size ARR_SIZE, initialized to zero
array<int, ARR_SIZE> cpp_arr = {}; //Size ARR_SIZE, initialized to zero
vector<int> vec_arr(ARR_SIZE); //Size ARR_SIZE, initialized to zero
cout << "Welcome to the Arrayitizer 2000(tm). Today we will be doing arrays three different ways.\n";
cout << "Please enter " << ARR_SIZE << " integers.\n";
//Now let's read into three arrays
for (size_t i = 0; i < ARR_SIZE; i++) {
int x;
cin >> x;
if (!cin) die();
c_arr[i] = x;
cpp_arr[i] = x;
vec_arr.at(i) = x;
}
//Let's print them out
cout << "You entered (one column for each of the three arrays):\n";
for (size_t i = 0; i < ARR_SIZE; i++) {
cout << c_arr[i] << "\t" << cpp_arr[i] << "\t" << vec_arr.at(i) << endl;
}
cout << "Summming up the three arrays:\n";
//Now lets sum them up and verify they all return the same value
int sum_c = sum_up(c_arr, ARR_SIZE);
int sum_cpp = sum_up(cpp_arr);
int sum_vec = sum_up(vec_arr);
cout << "sum_c: " << sum_c << endl;
cout << "sum_cpp: " << sum_cpp << endl;
cout << "sum_vec: " << sum_vec << endl;
if (sum_c == sum_cpp and sum_c == sum_vec) {
cout << "Congrats, you added up all three arrays the same!\n";
} else {
cout << "Unfortunately, your code for summing up the three arrays returned different results.\n";
}
}
Presumably, the following is a typo:
arr[i,size_t]
// ^^^^^^^
The compiler actually pointed you to exactly this code, so you could have spotted this. Or, at least, compared this part of the code with your friend's version (which cannot be "exactly like" yours if it compiles).
The warnings are just that: warnings. And they're warning you about what they say they're warning you about: variables that you declare but never use. So why declare them?
Finally, I don't understand why you say you're not "using std" — assuming you are not referring to the using namespace statement (because you are using that), I can only presume you think you're not using the standard library. Yet, you are! All over the place, in fact! Vectors, C++11 arrays, streams…
The compiler pointed you to the obvious errors:
arr[i, size_t] should be arr[i].
The first function loops ARR_SIZE times instead of using the size argument.
The second function loops ARR_SIZE times instead of using the size variable.
The third function loops ARR_SIZE times also, instead of using size computed as vec.size().
All three functions might produce the correct sum, but only by coincidence.
Here is a corrected version:
int sum_up(int arr[], size_t size) { //Function to sum up a C-style array
int sum = 0;
for (size_t i = 0; i < size; i++)
sum += arr[i];
return sum;
}
int sum_up(array<int, ARR_SIZE> arr) {
int sum = 0;
for (size_t i = 0, size = arr.size(); i < size; i++)
sum += arr[i];
return sum;
}
int sum_up(vector<int> vec) {
int sum = 0;
for (size_t i = 0, size = vec.size(); i < size; i++)
sum += vec[i];
return sum;
}