This code to find average of the negative elements works fine when I put everything in main. The problem is when I try to split it to functions. How can I connect the elements from cinarray and negative_average function?
#include <iostream>
using namespace std;
int main()
{
cinarray();
negative_average();
}
int cinarray()
{
int A[3][3];
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
cin >> A[i][j];
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
// compute average of only negative values
int negative_average()
{
int negCount = 0;
int average = 0;
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
if (A[x][y] < 0) {
++negCount;
average += A[x][y];
}
}
}
if (negCount > 0) {
average /= negCount;
cout << "Average of only negative values \n" << average;
}
}
}
and one more thing why the error list show that i need ";"
int negative_average()
{ //here
int negCount = 0;
int average = 0;
First of all, you cannot define a function inside another function's body, that's why the "; needed here" error. Move it to the global scope. In this case, you can create int A[3][3]; in main, and declare your functions accordingly:
void cinarray(int A[3][3]); // why int return type?
void negative_average(const int A[3][3]);
Then pass A to both.
As an option define array in main of the and pass reference to cinarray() and negative_average().
Do something like this:
int main()
{
int A[3][3];
cinarray(A);
negative_average(A);
return 0;
}
Where:
int cinarray(int (&A)[3][3])
int negative_average(const int (&A)[3][3])
Your array A isn't visible to both functions. You need to declare it in your main(), then pass it as an argument to your other functions.
Related
I have a question about the exercise from my course:
Write a program that takes array of real numbers as parameter and replaces each element that is smaller than average of the first and last element, by this average. This is my code:
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i; i < 6; i++) {
cout << "Enter the numbers" << endl;
cin >> arr[i];
}
int f = arr[0];
int l = arr[n - 1];
double av = f + l / 2;
for (int i; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n;
int arr[n];
replaverage(arr, n);
cout << arr << " " << endl;
return 0;
}
Code is working, however as an output, it is giving some kind of address "0x7fff2306a5c0". I'm beginner so I apologize, maybe my error is stupid but I don't know how to fix it. Thanks for helping!
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << "Enter the number: ";
cin >> arr[i];
cout << endl;
}
int f = arr[0];
int l = arr[n - 1];
double av = (f + l) / 2;
for (int i = 0; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n = 6; // Making 6 since you had it hardcoded
int arr[n];
replaverage(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
return 0;
}
First problem: Initialize your loop counters to 0;
Second problem: Initialize n in main being passed as parameter to
something
Third problem: Your average calculation is incorrect. It should be (f+l) / 2. Otherwise it will be doing l/2 + f, which is incorrect.
Fourth problem: You need to loop over your array to see all the
elements
I would like to point out some random integers using the regular print function, then print again the same integers using pointer notation. When I use pointer notation I run into some trouble. If anyone could send some tips it'd be much appreciated. If i comment out a specific line of code, the program will compile completely, but not with the outputs I'd like.
#include <iostream>
#include <ctime>
#include <stdlib.h> //srand(), rand()
using namespace std;
void printArray(int[], int);
//void printToday(int , );
int main()
{
int i = 0;
const int SZ = 100;
int myArray[SZ] ={0};
srand(time(0));
int myArrayTotal = 0;
int *thelight;
thelight = myArray;
for (int i = 0; i <=100; i++)
{
myArray[i]= i+rand()%1000 ;
}
cout << "Array Notation:\n\n";
printArray(myArray, SZ);
system("pause");
system("cls");
cout << "Pointer Notation: \n\n";
int k = 0;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
cout<< *(thelight + k)<< "\t";
++k; //if I comment out this line the second part of the program will run, but it isn' the values I want.
} cout<< endl;
}
}
void printArray(int ArrayName[], int ArraySize)
{
int k = 0;
for (int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10 ; ++j)
{
cout << ArrayName[k] << "\t";
++k;
}cout << endl;
}
}
Thank you
I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}
I'm facing some difficulties with pointers for 3D arrays in C++. I've a array of Q[3][N][N] which I want to pass to a function to print the values at [0][i][j] location (and also for [1][i][j] and [2][i][j]). How can I achieve this? Will it be more convenient to use Q[i][j][0] etc?
for 2D, the following piece of code works just fine when I give &Q[0][0] to the *var:
template <typename T>
void print2d(T *var, int I, int J){
cout << endl;
for (int j = 0; j < J; j++){
for (int i = 0; i < I; i++){
cout << setprecision(3) << setw(12) << *(var + (N*i + j));
}
cout << endl;
}
cout << endl;
}
I'm using the same approach to write a similar function for 3D which does not write out the correct values:
Can anybody let me know the correct way to point to the correct address of Q[i][j][1]. In input argument, I'm giving the address of Q[0][0][0]. Should I use different addresses (such as Q[i][j][1]) if I want to write out for that particular value of k?
template <typename T>
void print3d(T *var, int I, int J, int K){
cout << endl;
for (int j = 0; j < J; j++){
for (int i = 0; i < I; i++){
cout << setprecision(3) << setw(12) << *(var + I*J*i + I*j + K);
}
cout << endl;
}
cout << endl;
}
Sample example, N must be const.
But consider using vector.
#include <stdio.h>
#include <iostream>
using namespace std;
const int N = 5;
void func2(int* tab, int A, int B, int C)
{
printf("%d\n", tab[N*N*A + N*B + C] );
}
void func(int tab[3][N][N])
{
for (int i = 0; i < 3; ++i)
for (int j = 0; j < N; ++j, printf("\n"))
for (int k = 0; k < N; ++k)
{
printf("%d ", tab[i][j][k]);
}
}
int main()
{
int tab[3][N][N];
int p = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < N; ++j)
for (int k = 0; k < N; ++k)
{
tab[i][j][k] = p++;
}
func(tab);
printf("\n");
func2((int*)tab, 1, 1, 3);
}
I agree with Adam's answer but cant comment, need more points there.
1) Arrays are not pointers. Multidimensional arrays cannot be passed via a pointer, needs to have all but one dimension constant. Here is an answer to help understand.
Cannot cast array to pointer
How do I pass a reference to a two-dimensional array to a function?
http://www.cplusplus.com/forum/articles/17108/
You might want to try vectors from STL instead.
2) Templates are compiled only when there is an instance of it used, that is why its declaration and usage is generally put in one file.
Template Compilation
I am trying to create some test cases for my 'minimum dot product' problem. I want 10 test cases , each generating different set of values for both vector a and b.
The Problem is that even after using srand( time( NULL ) ) though a new input is generated every time I compile and run the code but that same input is used for all the 10 test cases.
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using std::vector;
void sort_asc(vector<int> &manav, int sizes)
{
int temp = 0;
for (int i = 0; i<sizes; i++)
{
for (int j = i + 1; j<sizes; j++)
{
if (manav[i] > manav[j])
{
temp = manav[i];
manav[i] = manav[j];
manav[j] = temp;
}
}
}
std::cout << "b in asc order : ";
for (int i = 0; i<sizes; i++)
{
std::cout << manav[i] << " ";
}
std::cout << std::endl;
}
void sort_desc(vector<int> &manav, int sizes)
{
int temp = 0;
for (int i = 0; i<sizes; i++)
{
for (int j = i + 1; j<sizes; j++)
{
if (manav[i] < manav[j])
{
temp = manav[i];
manav[i] = manav[j];
manav[j] = temp;
}
}
}
std::cout << "a in desc : ";
for (int i = 0; i<sizes; i++)
{
std::cout << manav[i] << " ";
}
std::cout << std::endl;
}
long long min_dot_product(vector<int> a, vector<int> b, int sizes) {
long long result = 0;
sort_desc(a, sizes);
sort_asc(b, sizes);
for (size_t i = 0; i < sizes; i++) {
result += a[i] * b[i];
}
return result;
}
int main() {
srand(time(NULL));
/*
std::cin >> n;
vector<int> a(n), b(n);
for (size_t i = 0; i < n; i++) {
std::cin >> a[i];
}
for (size_t i = 0; i < n; i++) {
std::cin >> b[i];
}
*/
//================================================================ TESTING =========================================================================
int z = 0;
int n = (rand() % 10) + 1; // generating the size of the vectors [1-10]
std::cout << "n = " << n << "\n";
vector<int> a;
vector<int> b;
while (z != 10) {
for (int i = 0; i < n; ++i)
{
int p = (rand() % 10) - 5;
a.push_back(p); // input values [-5,4] in 'a'
}
std::cout << "Unsorted Vector a = ";
for (int i = 0; i<n; i++)
{
std::cout << a[i] << " ";
}
std::cout << std::endl;
for (int i = 0; i < n; ++i)
{
int q = (rand() % 10) - 5;
b.push_back(q); // inputing values [-5,4] in 'b'
}
std::cout << "Unsorted Vector b = ";
for (int i = 0; i<n; i++)
{
std::cout << b[i] << " ";
}
std::cout << std::endl;
std::cout << "min_dot_product = " << min_dot_product(a, b, n) << std::endl;
z++;
}
return 0;
}
I somehow want to generate a different set of values for vector a and b for all of the 10 test cases every time I run the code.
I have tried srand(i) within the respective for loops before pushing the value in vectors but its not working for me, also reusing srand( time( NULL ) ) within the for loops is not gonna help either. Is there some other simple way I can achieve this?
The problem is you never clear out the vector on each iteration. Since you don't all of the new random numbers you generate are being added to the end of the vector and you ignore them since n never changes.
What you need to do is add
a.clear();
b.clear();
to the end of the while loop. This will clear out the vectors and then when you start the next iteration the new random numbers will get added into the part of the vector you use in your functions.
You could also set the vector the proper size and then use [] to access the elements. This way you would just overwrite the previous values and you would not have to call clear()
vector<int> a(n);
vector<int> b(n);
//...
for (int i = 0; i < n; ++i)
{
a[i] = (rand() % 10) - 5;
b[i] = (rand() % 10) - 5;
}
I put both assignments in the same for loop to save space. You can do this in two separate loops but it is not needed.