Passing an array into a function, nothing outputting - c++

I am trying to create something that fills in an array with 100 random numbers between 1 and 100. This was working fine when it was in the main function, but when I put it in an int function nothing outputs; I must be missing something simple as I'm just beginning. What can I do to fix this?
#include "stdafx.h"
#include <time.h>
#include <math.h>
#include <iostream>
int arrayer(int ar[101], int i);
int main()
{
srand(time(0));
int ar[101];
for (int i = 1; i < 101; ++i)
{
int arrayer(int ar[101], int i);
}
return 0;
}
int arrayer(int ar[101], int i) {
ar[i] = rand() % 100 + 1;
if (ar[i] < 10) {
std::cout << i << ": " << "0" << ar[i] << std::endl;
}
else {
std::cout << i << ": " << ar[i] << std::endl;
}
return ar[i];
}

You're calling and declaring the function incorrectly. This is what it should look like:
#include "stdafx.h"
#include <time.h>
#include <math.h>
#include <iostream>
int arrayer(int ar[101], int i);
int main() {
srand(time(0));
int ar[101];
for (int i = 1; i < 101; ++i)
{
arrayer(ar, i);
}
return 0;
}
int arrayer(int* ar, int i) {
ar[i] = rand() % 100 + 1;
if (ar[i] < 10) {
std::cout << i << ": " << "0" << ar[i] << std::endl;
}
else {
std::cout << i << ": " << ar[i] << std::endl;
}
return ar[i];
}
Notice also that you're not using the return value, so you can omit that if it's not going to ever be used.
EDIT: You can actually replace the if-else for printing the value with this:
std::cout << i << ": " << setw(2) << setfill('0') << ar[i] << std::endl;
You will need to include <iomanip> to do that.

You are passing the arguments in a very wrong manner, the way of calling a function is way more different than declaring or defining it. You need to call the function as :
arrayer(ar, i);
Just pass the address of the array, ar as it is and the variable i.
Also, its better to have the function arrayer return void rather than int, since the array gets modified when passed to the function and the value gets printed in the function itself, so you don't need to return anything.

Well there is nothing in output because you never call the arrayer function!
In your loop, you just declare again the function. As the new declaration is compatible with the previous one, the compiler accepts it without error.
In order to actually call the function, you need an function call expression:
for (int i = 1; i < 101; ++i)
{
arrayer(ar, i);
}

Related

How to i get my Array to output its random generated values from 0 - 99

I wanted to make a function called fillArray to set up random values to the Array.
Then i wanted the function to output all the values but i kept getting values way above 99.
This is what i have written so far:
#include <ctime>
#include <iostream>
using namespace std;
void fillArray(){
srand(time(NULL));
const unsigned int sizeOfArray = 10; // this sets the array to the constant size of 10
int numberArray[sizeOfArray]; //this sets the array to the size of 'sizeOfArray' which is 10
for(int i = 0; i < sizeOfArray; i++)
{
numberArray[i] = rand() % 100;
cout << numberArray[i] << endl;
}
}
int main (){
void fillArray(int a[10], int randNum);
cout << numberArray[0] << " , " << numberArray[1] << " , " << numberArray[2] << " , " << numberArray[3] << " , " << numberArray[4] << " , " << numberArray[5] << " , " << numberArray[6] << " , " << numberArray[7] << " , " << numberArray[8] << " , " << numberArray[9] << '\n';
}
I understand the name numberArray hasnt been declared in the main function but just wondered what to do.
The fillArray() function is not being called. You declared fillArray as -
void fillArray(). But when you call fillArray it is called as -
fillArray(int a[10], int randNum). This line does not call the function declared earlier as the number of parameters is different. The original fillArray() function does not accept any parameters but it is called with two. Moreover, we never write the return type when we call a function. Call the function as fillArray() and it will work.
Remember that the function call and the function declaration should be exactly same.
Regarding the not declared in scope problem, your array is declared in the fillArray function and thus not accessible in the main method. To solve this either make your array global and declare it outside the function after the namespace line or just don't use it in the main function. Just call fillArray() from the main method and it will populate and print the array for you.
So, your error is simple:
You have declared a function in the main function like this:
int main()
{
void fillArray(int a[10], int random);
}
But it should have been
int main()
{
fillArray(int a[10], int random);
}
Which is calling the fillArray function
Also there are a few other things that i would like to add,
instead of fillArray taking nothing it should take an array like this
template<auto Size>
void fillArray(int (&arr)[Size])
{
// Implemenatation
}
and also, it should not take a random number the function can do that in the function body.
When you are printing your array, you should use a loop or a specialized function to do it like this:
template<auto Size>
auto PrintArray(int (&arr)[Size])
{
for(int i = 0; i < Size; i++)
{
std::cout << arr[i] << ", ";
}
std::cout << "\n";
}
So, your final code would be something like the following:
#include <ctime>
#include <iostream>
using namespace std;
template<auto Size>
void fillArray(int (&numberArray)[Size]){
srand(time(NULL));
for(int i = 0; i < Size; i++)
{
numberArray[i] = rand() % 100;
}
}
template<auto Size>
auto PrintArray(int (&arr)[Size])
{
for(int i = 0; i < Size; i++)
{
std::cout << arr[i] << ", ";
}
std::cout << "\n";
}
int main (){
int numberArray[10];
void fillArray<10>(numberArray);
PrintArray<10>(numberArray);
}

Why is Geany IDE NOT catching an error? EXIT_SUCCESS; needs correct header file, c++

On both windows and linux I am using Geany IDE and writing a c++ program. For some reason right now it is not catching an error on either linux/windows. I am using EXIT_SUCCESS and need to have the header file , right? also using copy function and it requires ? It only stops when I exclude and I discovered this because in my code I left it out by accident but it compiled, built and ran just fine.
It catches it when I use just G++, I'm not sure what is going on, could it be some setting I have in Geany?
Here is the code I'm working with
#include <iostream>
#include <algorithm>
#include <cassert>
//~ #include <cstdlib>
int main()
{
int xSize = 10;
int ySize = 50;
int xData[xSize];
int yData[ySize];
for(int i = 0; i < xSize; ++i) {
xData[i] = i;
std::cout << " First X Array value: "<< xData[i] << " " << std::endl;
}
std::cout << std::endl;
for(int i = ySize; i >= 0; --i) {
yData[i] = i;
std::cout << " First Y Array value: "<< yData[i] << " " << std::endl;
}
std::cout << std::endl;
std::copy(xData, xData+6,yData+42);
std::cout << "The copy function added it's first 6 values to the yData array starting at Y's 42nd array position" << std::endl;
for (int i = 42; i < ySize; ++i) {
std::cout << "First array value of Y is now: " << yData[i] << std::endl;
}
assert(yData[42]==0);
return EXIT_SUCCESS;
}
Thanks!

Why only the first element of the array is initialized to -1? while rest of them are 0 [duplicate]

This question already has answers here:
Initialization of all elements of an array to one default value in C++?
(12 answers)
Closed 4 years ago.
I've initialized arr to -1 when I print them every element is initialized to 0 except the first element.
This is the small code of a bigger problem. I'm just struck here
#include <bits/stdc++.h>
using namespace std;
int fibo()
{
int static arr[100] = {-1};
for (int i = 0; i < 100; ++i)
{
cout << "arr[" << i <<"] : " << arr[i] << endl;
}
return -2;
}
int main(void)
{
cout << "Result : " << fibo() << endl;
return 0;
}
Simplest solution -- use std::vector<int>, and the initialization of all elements becomes available to you in a very easy form (I know there are template tricks that can be done, but IMO there is no need for that level of complexity for this in your code).
Example:
#include <vector>
#include <iostream>
int fibo()
{
static std::vector<int> arr(100,-1);
for (int i = 0; i < 100; ++i)
{
std::cout << "arr[" << i <<"] : " << arr[i] << "\n";
}
return -2;
}
int main(void)
{
std::cout << "Result : " << fibo() << "\n";
return 0;
}
Live Example
#include <bits/stdc++.h>
using namespace std;
int fibo()
{
int static arr[100];
for (int i = 0; i < 100; ++i)
{
arr[i] = -1;
}
for (int i = 0; i < 100; ++i)
{
cout << "arr[" << i <<"] : " << arr[i] << endl;
}
return -2;
}
int main(void)
{
cout << "Result : " << fibo() << endl;
return 0;
}
Try using this code

Test an integer value to determine if it is odd or even in C++

I have to write a program to test an integer value to determine if it is odd or even, and make sure my output is clear and complete. In other words, I have to write the output like "the value 4 is an even integer". I was also hinted that I have to check the value using the remainder modulo.
The issue I have is with the scanf() function. I get a syntax error:
'%=' expected a ')'
How do I fix this?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
int scanf(%=2 , &number);
if (number == 0)
cout << "the value" << number << "is even";
else
cout << "the value" << number << "is odd";
return 0;
}
You are using scanf() incorrectly (read the scanf() documentation on cppreference.com). The first parameter expects a null-terminated string containing the format to scan, but you are not passing in anything that even resembles a string. What you are passing in is not valid string syntax, per the C++ language standard. That is why you are getting a syntax error.
You need to change this line:
int scanf(%=2 , &number);
To this instead:
scanf("%d", &number);
Though, in C++ you really should be using std::cin instead for input (you are already using std::cout for output):
std::cin >> number;
Try this:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
if (cin >> number)
{
if ((number % 2) == 0)
cout << "the value " << number << " is even";
else
cout << "the value " << number << " is odd";
}
else
cout << "the value is invalid";
return 0;
}
I know this question is a little dated, however, if you are able to use modern C++ features. You can write a constexpr helper function such as this:
#include <cstdint>
constexpr bool isEven(uint32_t value) {
return ((value%2) == 0);
}
Then in your main function, you can traverse through a loop of N integers and output your display such as:
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isEven(i) ? "even" : "odd") << '\n';
}
return 0;
}
It's literally that simple. Here's another nice feature of using the constexpr helper function... You can also format your output as such:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isEven(i) << '\n';
}
return true;
}
If you are looking for something that is more efficient than using the modulo operator you can bitwise & with the least significant digit... The code above would then become:
#include <cstdint>
constexpr bool isOdd(uint32_t value) {
return (value&1);
}
And using it would be very similar as above, just make sure you reverse the wording in your output to match that from the function being used...
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isOdd(i) ? "odd" : "even") << '\n';
}
return 0;
}
Again you can use the std::boolalpha manipulator to get this kind of output:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isOdd(i) << '\n';
}
return true;
}

Visual studio: E0349 no operator << matches these operands (but no strings in code)

Can't compile my code - I get: no operator “<<” matches these operands
I found similar problem no operator "<<" matches these operands, however I have no strings nor missing directives (i think)
Could someone help me please? :)
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
void convBase(int base, int n);
using std::cout;
using std::endl;
using std::setw;
using std::vector;
int main()
{
int numbs[] = { 61, 0, -9, -200, 9999 };
int bases[] = { 16, 8, 2, 20, 36 };
size_t size = sizeof(numbs) / sizeof(*numbs);
for (size_t i = 0; i < size; ++i) {
cout << setw(4) << numbs[i] << " = " << setw(5)
<< convBase(numbs[i], bases[i]) << " in base "
<< setw(2) << bases[i] << endl;
}
return 0;
}
void convBase(int n, int base) {
char pierwszyZnak = 48;
char pierwszaLitera = 65;
vector<char> system;
vector<char> liczba;
for (int i = 0; i < base; i++) {
if (i <= 9) {
system.push_back(pierwszyZnak);
pierwszyZnak++;
}
else if (i <= 36) {
system.push_back(pierwszaLitera);
pierwszaLitera++;
}
else {
cout << "podales za duza liczbe: " << base << ". Musisz podac liczbe mniejsza badz rowna 36" << endl;
return;
}
}
while (n > 0) {
int rem = n % base;
int rem2 = floor(n / base);
liczba.push_back(system[rem]);
n = rem2;
}
for (unsigned int i = liczba.size(); i-- > 0; )
std::cout << liczba[i];
}
convBase returns void, but you're trying to stream its return value to std::cout. Your function should return a string representation, silently. Return a std::string or std::ostream instead of void.
I'd suggest creating a std::stringstream and then streaming your output to that. You can replace the very last std::cout with the name of your stream, and then call its str() method to get the return value. (You should also make sure to verify the function behaves predictably when you give it bad characters.)