2 Dimension Passing (missing subscript, unknown size) - c++

I am constantly getting a missing subscript and unknown size problem. So, I am guessing this is a beginner problem but I can't get my head around it. How do I get my function to work and outputted to the screen?
I am trying to have two columns be filled with numbers. Column[0] is inputted by rand() and then have Column[1] be converted into a new number through an equation. I am expecting 1-10 rows to be inputted.
// function prototypes
void arrayProgram(double ma[][2], int xSize);
int main()
{
const int arraySize = 5;
double ma[arraySize][arraySize];
// if I change double ma[1][2];
// I get an argument of type 'int' is incompatible of type "double(*)[2]
arrayProgram(ma, arraySize);
}//end main
void arrayProgram(double ma[][2], int xSize)
{
int i = 0;
for (i = 0; i < xSize; ++i)
{
ma[i][0] = rand();
ma[i][1] = (ma[i][0] * (20 / 25.0) + 64);
}
}

It works:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
void arrayProgram(double ma[][2], int xSize);
int main()
{
const int arraySize = 1;
double ma[arraySize][2];
srand ( time(NULL) ); // setting seed value
rand(); // first random number
arrayProgram(ma, arraySize);
}//end main
void arrayProgram(double ma[][2], int xSize)
{
int i = 0;
for (i = 0; i < xSize; ++i)
{
ma[i][0] = rand();
ma[i][1] = (ma[i][0] * (20 / 25.0) + 64);
std::cout << ma[i][0] << '\t' << ma[i][1] << std::endl;
}
}

Related

C++ Array of random numbers

I have a bit of a problem with this. I've tried to create a function to return a random number and pass it to the array, but for some reason, all the numbers generated are "0".
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int generLosNum(int);
int main()
{
srand(time(NULL));
int LosNum;
const int rozmiar = 10;
int tablica[rozmiar];
for(int i=0; i<rozmiar; i++)
{
tablica[i] = generLosNum(LosNum);
cout << tablica[i] <<" ";
}
return 0;
}
int generLosNum(int LosNum)
{
int LosowyNum;
LosowyNum = (rand() % 10);
return (LosNum);
}
So the return for your int generLosNum(int LosNum) was printing 0 because you had it returning LosNum which was initialized equaling to zero. I changed your code so it works and will print out the 10 random numbers.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int generLosNum();
int main()
{
srand(time(NULL));
int LosNum = 0;
const int rozmiar = 10;
int tablica[rozmiar];
for (int i = 0; i < rozmiar; i++)
{
tablica[i] = generLosNum();
cout << tablica[i] << " ";
}
return 0;
}
int generLosNum()
{
int LosowyNum;
LosowyNum = (rand() % 10);
return LosowyNum;
}
Change your method generLosNum to the following and the method signature to int generLosNum() and it should work.
int generLosNum()
{
return (rand() % 10);
}
Reason: As others also mentioned in the comments, you were just returning the number that you passed in as parameter and also the logic for this method doesn't even need a parameter.

function was not declared in this scope? [duplicate]

This question already has answers here:
Why is my HelloWorld function not declared in this scope?
(11 answers)
Closed 5 years ago.
I'm trying to write a program that displays asterisks and spaces based on their input and I've run into a compiler problem:
chart.cpp:24:41: error: ‘find_largest’ was not declared in this scope
int largest = find_largest(values, size);
This is my code:
/*
* Project 1
* Author: Erik Ingvoldsen
* Date: 2/1/2018
*/
#include <iostream>
using namespace std;
int size = 0; //initalizing "size" at 0.
const int MAX = 100; //setting max value
int values[MAX]; //100 int limit
int main(){
int num;
for (int i = 0; i < MAX; i++) {
cin >> num; //allow the user to put in a number
values[i] = num; //assigning value to the array
if (num <= 0) {
break; //stop if "0" or lower is entered
}
size++; //increase the size of array, assuming the for loop hasn't been broken
}
int largest = find_largest(values, size); //setting the amount of rows
for (int i = 0; i < size; i++) {
if (values[i] = largest) {
cout << "*"; //if the value of the area reachest the highest row, give a *
} else {
cout << "\n"; //otherwise just give a blank space
}
largest--; //by shrinking "largest", we move down the next row
cout << endl;
}
return 0;
}
int find_largest(int values[], int size) {
int largest = 0;
for (int i = 0; i < size; i++) {
if (values[i] > largest) {
largest = values[i]; //if the value of the array is bigger than the current largest it is replace
}
}
return largest; //once the for loop is completed, it returns the largest number found
}
I really can't tell the difference between this and the function...and I'm pretty sure I'm not supposed to declare it as "int largest = find_largest(values[], size);"
You are supposed to declare all functions before you use them. Simple way to do that is to use a prototype.
// prototype
int find_largest(int values[], int size);
int main(){
...
}
int find_largest(int values[], int size) {
...
}

POSIX pthread_create scrambles the values of variables in a struct, how to avoid that?

So I have my program here:
#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int const size = 3;
struct Arguments{
int array[];
float result1[];
float result2[];
};
//void calc(int arr[], float rarr1[], float rarr2[], int size);
void* calc(void *param);
int main(int argc, char *argv[]){
time_t t;
srand((unsigned) time(&t));
int arr[size][size] = {};
float rarr1[size][size-1] = {};
float rarr2[size][size-1] = {};
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
int number = rand()%10;
arr[x][y] = number;
}
}
for(int x = 0; x < size; x++){
for(int y = 0; y < size; y++){
cout << arr[x][y] << " ";
}
cout << endl;
}
cout << endl;
/////////////////////////////////////////
pthread_t child;
struct Arguments input;
for(int i = 0; i < size; i++){
input.array[i] = arr[0][i];
}
pthread_create(&child, NULL, calc, (void*)&input);
pthread_join(child, NULL);
//calc(&input);
for(int i = 0; i < size-1; i++){
rarr1[0][i] = input.result1[i];
cout << "Test: " << rarr1[0][i] << endl;
}
//////////////////////////////////
return 0;
}
//void calc(int arr[], float rarr1[], float rarr2[], int size){
void* calc(void *param){
struct Arguments *input = (struct Arguments*)param;
int arr1[] = {};
float rarr1[] = {};
float rarr2[] = {};
for(int i = 0; i < size; i++){
arr1[i] = input->array[i];
}
for(int i = 0; i < size; i++){
int a = arr1[i];
int b = arr1[i+1];
int difference = a-b;
if(difference < 0){
difference = difference * -1;
}
float euc = 1 + pow(difference, 2);
euc = sqrt(euc);
rarr1[i] = euc;
}
for(int i = 0; i <size-1; i++){
input->result1[i] = rarr1[i];
}
for(int i = 0; i <size-1; i++){
int a = arr1[i];
int b = arr1[i+1];
int difference = a-b;
if(difference < 0){
difference = difference * -1;
}
float apar = (difference/rarr1[i]);
float result = asin(apar);
result = result*(180/3.14);
rarr2[i] = result;
}
return NULL;
}
The important part that causes the trouble is between ////// lines but I left the rest of the code for the context, since it might be useful.
So I have the function calc(param); that does the important calculation in the program.
It is working just fine as long as I call it myself (by actually including the function call in the code) and the test loop right after it gives the correct results.
However, when I try to use pthread_create(); to create a new thread that will take care of executing that function, the test loop spits out nonsense and some random huge numbers different each time.
It's kinda weird because the code compiles either way, and literally the only thing that I change is these 2 lines.
What am I doing wrong and why the function spits out garbage when started by the Pthread? Is there a way to fix it?
Ok so if anyone's having a similar problem:
Declare the size of arrays no matter what. It turns out that my program didn't work properly because I initialized my result arrays as float result1[]; instead of float result1[size];

C++ Turning arrays into functions?

I have been attempting this for hours to no avail, as you can see in my code I have separate functions, they were all together in main, but I am required to turn each into a separate function. However when I try anything I get errors, even when I try to pass parameters. Can someone point me in the right direction?
#include <iostream>
#include <cstdlib>
#include <ctime>
void printarray();
void average();
void largestnumber();
using namespace std;
int main()
{
printarray();
average();
largestnumber();
}
void printarray() {
srand(time(0));
int n[10], tot = 0;
for (int i = 0; i < 10; i++)
{
n[i] = (1 + rand() % 100);
cout << n[i] << endl;
}
}
void average() {
int j, tot = 0, n[10];
for (j = 0; j < 10; j++)
{
tot += n[j];
}
cout << "The average of the numbers in the array are " << tot / j << endl;
}
void largestnumber() {
int w = 1, int n[10];
int temp = n[0];
while (w < 10)
{
if (temp < n[w])
temp = n[w];
w++;
}
cout << "The largest number in the array is " << temp << endl;
}
The array you are working with needs to be passed in to each function, so the same array is used everywhere. It is a good idea to pass the size as well, just for flexibility reasons.
Now your functions pretty much work as you wrote them.
#include <iostream>
#include <cstdlib>
#include <ctime>
void printarray(int n[], size_t size);
void average(int n[], size_t size);
void largestnumber(int n[], size_t size);
using namespace std;
int main()
{
const size_t arr_size = 10;
int n[arr_size];
printarray(n, arr_size);
average(n, arr_size);
largestnumber(n, arr_size);
}
void printarray(int n[], size_t size) {
srand((unsigned int)time(0));
int tot = 0;
for (size_t i = 0; i < size; i++)
{
n[i] = (1 + rand() % 100);
cout << n[i] << endl;
}
}
void average(int n[], size_t size) {
size_t j;
int tot = 0;
for (j = 0; j < size; j++)
{
tot += n[j];
}
cout << "The average of the numbers in the array are " << tot / j << endl;
}
void largestnumber(int n[], size_t size) {
size_t w = 1;
int temp = n[0];
while (w < size)
{
if (temp < n[w])
temp = n[w];
w++;
}
cout << "The largest number in the array is " << temp << endl;
}
One simple improvement is to break the printarray out into an initarray function that fills the array and printarray that prints the content.
It would also be a good idea to do some checking for things like an empty array (functions assume n[0] exists, for instance).
The next obvious step is to put all this in a class. Also, if you are allowed to, the c array should be replaced with a vector, as that does a great job of keeping all the resource information together.

Generating random non repeating number array in C++

I need to generate random non repeating number array in C++, in this part of code I generate random numbers using, srand function, but some of the numbers are repeating. The main task is to generate random numbers for lottery ticket, so I need to generate numbers until golden number which is marked as int golden.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
int golden = 31;
int i = 0;
int array[35];
srand((unsigned)time(0));
while(i != golden){
array[i] = (rand()%75)+1;
cout << array[i] << endl;
i++;
}
}
One strategy is to populate an array with numbers from 1 to 75, and then use std::random_shuffle() on it. You can then read the numbers from the array until you hit the golden number.
I had a similar task and used two functions to solve the problem of repeating numbers.
#include <iostream>
#include <ctime>
using namespace std;
void generateRandom(int array[], int length);
bool findVal(int array[], int size, int value);
int main() {
int arraySize = 10;
int array[arraySize];
generateRandom(array, arraySize);
for (auto i : array) {
cout << i << " ";
}
return 0;
}
void generateRandom(int array[], int length) {
srand((int) time(nullptr));
int temp;
for (int i = 0; i < length; ++i) {
temp = rand() % 20 + 1;
if (findVal(array, i, temp)) {
i--;
continue;
} else {
array[i] = temp;
}
}
}
bool findVal(int *array, int size, int value) {
for (int i = 0; i < size; ++i) {
if (array[i] == value) {
return true;
}
}
return false;
}
Within the generateRandom function, you can switch the 20 and 1 used in the for loop with your preferred upper and lower limits respectively.