I'm trying to call the function
int Even( int a[][MAX_COLUMNS], int length, int width)
inside the main to print the output to the user.
After running the program, i got the following error
`int Even(int,int,int) connot convert argument 1 from int [3][2] to int
Here's the code,
Thank you for taking the time to help me.
#include <iostream>
#define MAX_ROWS 3
#define MAX_COLUMNS 2
using namespace std;
int Even(int A, int length, int width);
int main(){
int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } };
cout << "There are " << Even(A,MAX_ROWS,MAX_COLUMNS) << " even numbers in the matrix." << endl;
cout << "Goodbye :-)" << endl;
system("PAUSE");
return 0;
}
int Even( int a[][MAX_COLUMNS], int length, int width){
int sum = 0;
int i;
int j;
for (i = 0; i < length, i++;){
for (j = 0; j < width, j++;){
if (a[i][j] % 2 == 0){
sum++;
}
}
}
return sum;
}
You should alwais avoid forward declaration (except special cases). At your Q it leaded you to error. Full code:
#include <iostream>
#define MAX_ROWS 3
#define MAX_COLUMNS 2
using namespace std;
int Even( int a[][MAX_COLUMNS], int length, int width){
int sum = 0;
int i;
int j;
for (i = 0; i < length, i++;){
for (j = 0; j < width, j++;){
if (a[i][j] % 2 == 0){
sum++;
}
}
}
return sum;
}
int main(){
int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } };
cout << "There are " << Even(A,MAX_ROWS,MAX_COLUMNS) << " even numbers in the matrix." << endl;
cout << "Goodbye :-)" << endl;
system("PAUSE");
return 0;
}
Related
I have been trying to sort an array using recursion, but I am getting no output, can somebody help me with this, like why there is no output?
void sortarray(int arr[], int index, int key, int len){
if (index == len-2){
}
else{
if (key==len-1){
key=0;
sortarray(arr, index++, key, len );
}
else {
if (arr[key] > arr[key+1]){
swap(arr[key], arr[key+1]);
}
sortarray(arr, index, key++, len );
}
}
}
int main(){
int arr[5] = {5,6,4,3,2};
sortarray(arr, 0,0,5);
for(int i=0; i<5; i++){
cout << arr[i] << " ";
}
}
I fixed the segfault and simplified the code to make it more clear what it does. Namely, walks the array once and swap the key with the next if it's larger. This is not a valid sort algorithm:
#include <iostream>
using namespace std;
void sortarray(int arr[], int key, int len) {
// base case
if (key + 1 == len) return;
// recursive case
if (arr[key] > arr[key + 1]){
swap(arr[key], arr[key + 1]);
}
sortarray(arr, key + 1, len );
}
int main() {
int arr[] = {5,6,4,3,2};
int len = sizeof(arr) / sizeof(*arr);
sortarray(arr, 0, len);
for(int i = 0; i < len; i++) {
cout << arr[i] << " ";
}
return 0;
}
and the result is:
5 4 3 2 6
I made some edits to your code and it works now (don't mind the printArr function, it's there just because cycles in main are ugly):
#include <iostream>
using namespace std;
void printArr(int arr[], int size)
{
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void sortarray(int arr[], int index, int key, int len)
{
if (index < len)
{
if (key == len - 1) {
key = 0;
sortarray(arr, ++index, key, len);
}
else {
if (arr[key] > arr[key + 1]) {
swap(arr[key], arr[key + 1]);
}
sortarray(arr, index, ++key, len);
}
}
}
int main()
{
int arr[5] = { 5,6,4,3,2 };
cout << "Before sort:" << endl;
printArr(arr, sizeof(arr)/sizeof(*arr));
sortarray(arr, 0, 0, 5);
cout << "After sort:" << endl;
printArr(arr, sizeof(arr) / sizeof(*arr));
return 0;
}
So the first problem was missing iostream and std namespace.
Key changes in the algorithm itself were:
change argument++ to ++argument
change the first condition to (index < len)
As to why your code didn't work properly: the index missed the stopping condition and went over the value of len (thus the screen o' fives).
So this answers your question, but this algorithm you wrote breaks once there are two same values next to each other. There is a good reason we use cycles for sorting and unless recursion is the point, I would recommend abandoning it for this task.
This code works perfectly-:
void sortArr(int arr[],int index, int key, int len){
// static int index = 0;
// static int key = 0;
if (index == len-1){
}
else{
if (key==len-1){
key=0;
sortArr(arr,index+1,key,len );
}
else {
if (arr[key] > arr[key+1]){
swap(arr[key], arr[key+1]);
}
sortArr(arr, index, key+1 ,len );
}
}
}
int main(){
int arr[5] = {5,6,4,3,2};
sortarray(arr, 0, 5);
for(int i=0; i<5; i++) {
cout << arr[i] << " ";
}
return 0;
}
The output of the code is:
2 3 4 5 6
should be like this output:
Monday
onday
nday
day
ay
y
what I have so far:
#include <iostream>
using namespace std;
int main () {
char *weekDays[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
for (int i=0;i<7;i++){
cout << weekDays[0][i] << endl;
}
return 0;
}
output:
M
o
n
d
a
y
Im on mobile and tired but sth. like this should work:
int main(){recPrint(0);}
void recPrint(int level){
char mon[] = "Monday";
for(int i=level; i<strlen(mon); i++){
std::cout << mon[i];
}
std::cout << std::endl;
recPrint(++level);
}
add another parameter char dayOfTheWeek[] and you can call it with whatever you want.
Try this
#include <iostream>
using namespace std;
void display(char s[])
{
int n = strlen(s);
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
cout << s[j];
}
cout << endl;
}
}
int main () {
char *weekDays[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
display(weekDays[0]);
}
I'm trying to write a function that calculates the sum of an array, but when i declare int size = 0; , the function runs 0 times because i=0 ; i
int arraChec(int arra[]) {
int size = 0;
int sum = 0;
for (int i = 0; i < size; i++) {
sum = sum + arra[i];
}
return sum;
}
int main() {
int arra1[7] = { 2,3,5,7,8,9,1 };
cout << arraChec(arra1) << endl;
system("pause");
}
Pass in the array size as a parameter:
#include <iostream>
int arraChec(int arra[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arra[i];
}
return sum;
}
int main() {
int arra1[7] = { 2, 3, 5, 7, 8, 9, 1 };
std::cout << arraChec(arra1, 7) << std::endl;
}
Or use std::vector:
#include <iostream>
#include <vector>
int arraChec(std::vector<int>& arra) {
int sum = 0;
for (int i = 0; i < arra.size(); i++) {
sum += arra[i];
}
return sum;
}
int main() {
std::vector<int> arra1 = { 2, 3, 5, 7, 8, 9, 1 };
std::cout << arraChec(arra1) << std::endl;
}
If you are referring to some C style (sizeof(arra) / sizeof(*arra)) construct I suggest you refrain from using it.
You need to pass two arguments to the function--either the beginning of the array plus the size, or the beginning and (one past the) end, as is conventional in C++:
int arraChec(int* begin, int* end) {
int sum = 0;
for (int* it = begin; it < end; ++it) {
sum += *it;
}
return sum;
}
int main() {
int arra1[7] = { 2,3,5,7,8,9,1 };
cout << arraChec(std::begin(arra1), std::end(arra1)) << endl;
system("pause");
}
Of course, you can implement is using the standard library:
cout << std::accumulate(std::begin(arra1), std::end(arra1), 0) << endl;
Use std::array instead of fixed size C-style array.
#include <iostream>
#include <array>
#include <numeric>
using namespace std;
int main() {
array<int, 7> arr = { 2, 3, 5, 7, 8, 9, 1 };
cout << accumulate(arr.begin(), arr.end(), 0) << endl;
return 0;
}
Output
35
Read more about std::accumulate.
Another way not mentioned yet is:
template<size_t N>
int arraChec(int (&arra)[N]) {
int sum = 0;
for (size_t i = 0; i < N; i++) {
sum = sum + arra[i];
}
return sum;
}
My professor gave us an example main\source.cpp file and wants us to create the necessary class\header file to make it work. He's the no example code vague "helpful hints" type so I'm lost.
I know this is a long post any input anyone may be willing to help me with at all would be a huge help.
Source.cpp he provided;
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include "DataBuffer.h"
using namespace std;
void testDataBuffer(int arr[], int length);
int main() {
const int ARR0_LEN = 2;
int arr0[ARR0_LEN] = { -2,-1 };
const int ARR1_LEN = 10;
int arr1[ARR1_LEN] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const int ARR2_LEN = 25; int arr2[ARR2_LEN] = { 2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22 };
testDataBuffer(arr0, ARR0_LEN);
testDataBuffer(arr1, ARR1_LEN);
testDataBuffer(arr2, ARR2_LEN);
//hold console open
std::cin.get();
return 0;
}
void testDataBuffer(int arr[], int length) {
DataBuffer buf; buf.copyData(arr, length);
buf.print(); cout << "Sum " << buf.sum() << endl;
cout << "Min " << buf.minValue() << endl;
cout << "Max " << buf.maxValue() << endl;
cout << "Range " << buf.range() << endl;
cout << "Mean " << buf.mean() << endl;
}
My DataBuff.h
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#pragma once
class DataBuffer {
static const int BUFFER_SIZE = 256;
int buffer[BUFFER_SIZE];
static const int length = 5;
int arr[length] = { 1,2,3,4,5 };
public:
bool copyData(int intArray, int length);
string print();
double mean(int sum);
int sum();
int maxValue();
int minValue();
int range(int small, int large);
};
My DataBuffer.cpp
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include <array> // .sizeof()
#include "DataBuffer.h"
using namespace std;
bool DataBuffer::copyData(int arr, int length) {
for (int i = 0; i < length; i++) {
arr = buffer[i];
}
cout << "Length of buffer is " << length;
if (sizeof(buffer) < length) {
return false;
}
else {
return true;
}
}
string DataBuffer::print() {
if (length <= 0) {
cout << "{}" << endl;
}
else {
cout << buffer[0];
for (int i = 1; i < length; i++) {
cout << setw(10) << buffer[i];
cout << endl;
}
}
}
int DataBuffer::sum()
{
int i, sum = 0;
for (i = 0; i < length; i++)
{
sum += buffer[i];
}
return sum;
}
double DataBuffer::mean(int sum) {
double mean = sum / length;
return mean;
}
int DataBuffer::maxValue() {
int i = 0;
int large = buffer[0];
for (i = 1; i < length; i++)
{
if (buffer[i] > large)
large = buffer[i];
}
return large;
}
int DataBuffer::minValue() {
int i;
int small = buffer[0];
for(i=1; i < length; i++)
{
if(buffer[i] < small)
small = buffer[i];
}
return small;
}
int DataBuffer::range(int small, int large)
{
int range = large - small;
return range;
}
Main errors I'm stuck on;
The professor states that range and mean should accept parameters, so why doesn't the method call in his cpp have parameters? I'm getting errors stating range doesn't take 0 arguments
Error C3646 'print': unknown override specifier AustinNorrisBuffer c:\users\austinn\downloads\austinnorrisbuffer\databuffer.h 19
Error C2039 'print': is not a member of 'DataBuffer' AustinNorrisBuffer c:\users\austinn\downloads\austinnorrisbuffer\source.cpp 29
Again long post I know, any help appreciated!
I suggest the following approach (so that you learn what your professor had in mind and keep your sanity along the way):
Comment everything out.
Uncomment selected pieces of code.
Use a search engine to research those specific errors. Fix the code/header and proceed.
If you hit a very specific issue and need help then you can post a well formed question here and you will get a specific answer quickly.
Good luck.
Here is what I had to comment out to get it to compile at least:
DataBuff.h
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#pragma once
class DataBuffer {
static const int BUFFER_SIZE = 256;
int buffer[BUFFER_SIZE];
static const int length = 5;
int arr[length] ={1,2,3,4,5};
public:
bool copyData(int intArray,int length);
//string print();
double mean(int sum);
int sum();
int maxValue();
int minValue();
int range(int small,int large);
};
DataBuffer.cpp
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include <array> // .sizeof()
#include "DataBuff.h"
using namespace std;
bool DataBuffer::copyData(int arr,int length)
{
for (int i = 0; i < length; i++) {
arr = buffer[i];
}
cout << "Length of buffer is " << length;
if (sizeof(buffer) < length) {
return false;
}
else {
return true;
}
}
//string DataBuffer::print()
//{
//if (length <= 0) {
// cout << "{}" << endl;
//}
//else {
// //cout << buffer[0];
// for (int i = 1; i < length; i++) {
// //cout << setw(10) << buffer[i];
// cout << endl;
// }
//}
//}
int DataBuffer::sum()
{
int i,sum = 0;
for (i = 0; i < length; i++) {
sum += buffer[i];
}
return sum;
}
double DataBuffer::mean(int sum)
{
double mean = sum / length;
return mean;
}
int DataBuffer::maxValue()
{
int i = 0;
int large = buffer[0];
for (i = 1; i < length; i++) {
if (buffer[i] > large)
large = buffer[i];
}
return large;
}
int DataBuffer::minValue()
{
int i;
int small = buffer[0];
for (i=1; i < length; i++) {
if (buffer[i] < small)
small = buffer[i];
}
return small;
}
int DataBuffer::range(int small,int large)
{
int range = large - small;
return range;
}
Source.cpp
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include "DataBuff.h"
using namespace std;
void testDataBuffer(int arr[],int length);
int main()
{
const int ARR0_LEN = 2;
int arr0[ARR0_LEN] ={-2,-1};
const int ARR1_LEN = 10;
int arr1[ARR1_LEN] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int ARR2_LEN = 25; int arr2[ARR2_LEN] ={2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22};
testDataBuffer(arr0,ARR0_LEN);
testDataBuffer(arr1,ARR1_LEN);
testDataBuffer(arr2,ARR2_LEN);
//hold console open
std::cin.get();
return 0;
}
void testDataBuffer(int arr[],int length)
{
DataBuffer buf;
//buf.copyData(arr,length);
//buf.print(); cout << "Sum " << buf.sum() << endl;
//cout << "Min " << buf.minValue() << endl;
//cout << "Max " << buf.maxValue() << endl;
//cout << "Range " << buf.range() << endl;
//cout << "Mean " << buf.mean() << endl;
}
My waitSeconds() functions takes an integer for the number of seconds to wait. I am using the to use Sleep(msec)and converting to seconds at this point I want to try doing it like this and know it's not elegant. However my programs does not execute the rest of my functions calls and I am head banging.
Ultimately, what I want to use this functions call for is to call it with my slowTriangle() function and distressCall() that loops forever with a pause of what the parameter of waitSeconds has been passed through. I hope this last part is making sense. Anyways thank you for any guidance any of you experienced members can provide.
#include <iostream>
#include <Windows.h>
using namespace std;
int dots(int count);
int dashes(int count);
void sendSOS();
void waitSeconds(int seconds2Wait);
int triangle(int rows);
int slowTriangle();
void distressCall();
int main()
{
dots(3);
dashes(3);
sendSOS();
cout << "\n\n ";
waitSeconds(1);
int triangle(4);
int slowTriangle();
void distressCall();
return 0;
}
int dots(int count) // counts DOWN the number of dots that the int is set as a parameter
{
for (; count >= 1; count--)
{
cout << "." ;
}
return 0;
}
int dashes(int count) // counts UP the number of dots that the int is set as a parameter
{
int i;
for (; count >= 1; count--)
{
cout << "-";
}
return 0;
}
void sendSOS()
{
dots(3);
dashes(3);
dots(3);
}
void waitSeconds(int seconds2Wait) //Sleeps for time specified
{
Sleep(1000 * seconds2Wait); //converts miliseconds to seconds
seconds2Wait = 2;
}
int triangle(int rows) //Prints a dot triangle
{
int i, j;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
int slowTriangle(int rows) //Prints a dot triangle with sleep paramter passed in
{
int i, j, seconds2Wait;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
waitSeconds(3);
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
void distressCall()
{
sendSOS();
waitSeconds(2);
}
The correct C++ answer is already in the comments: std::this_thread::sleep_for
The WinAPI method (Sleep) is also possible.
The real reason why your "functions" appear to fail is because int triangle(4) defines a new variable in main, initialized to 4. This variable hides the global triangle function.
#include <ctime>
void pause (unsigned int seconds)
{
time_t goal = seconds + time(0);
while (goal > time(0));
}
#include <iostream>
#include <ctime>
using namespace std;
int dots(int count);
int dashes(int count);
void sendSOS();
void waitSeconds(int seconds2Wait);
int triangle(int rows);
int slowTriangle(int rows);
void distressCall();
int main()
{
dots(3);
dashes(3);
sendSOS();
cout << "\n\n";
waitSeconds(1);
triangle(4);
slowTriangle(4);
distressCall();
return 0;
}
int dots(int count) // counts DOWN the number of dots that the int is set as a parameter
{
for (; count >= 1; count--)
{
cout << "." ;
}
return 0;
}
int dashes(int count) // counts UP the number of dots that the int is set as a parameter
{
for (; count >= 1; count--)
{
cout << "-";
}
return 0;
}
void sendSOS()
{
dots(3);
dashes(3);
dots(3);
}
void waitSeconds(int seconds2Wait) //Sleeps for time specified
{
time_t goal = seconds2Wait + time(0);
while (goal > time(0));
}
int triangle(int rows) //Prints a dot triangle
{
int i, j;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
cout << ". ";
}
cout << "\n";
}
return 0;
}
int slowTriangle(int rows) //Prints a dot triangle with sleep paramter passed in
{
int i, j;
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
waitSeconds(3);
cout << ". ";
}
cout << "\n";
}
return 0;
}
void distressCall()
{
sendSOS();
waitSeconds(2);
}
Implemented pause function as suggested and it works as intended now. Kudos to our other friend who wrote the ctime pause function. I also fixed some of your syntax errors :)
If you want distressCall() to run forever make it like this:
void distressCall()
{
while(true)
{
sendSOS();
waitSeconds(2);
}
}
Strongly recommend that you implement an escape mechanism though :).