I keep getting 3 compiling errors that say "Function definition is not allowed". I am not sure on how to fix it. I keep getting it after the int main, and after the void functions. Please help!
This is the code:
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
int populateIntegerArray(int *arrayPtr, int arraySize);
void displayIntegerArray(int *arrayPtr, int arraySize);
int findMaximumInteger(int *arrayPtr, int arraySize);
int populateIntegerArray(int *arrayPtr,int arraySize)
{
for(int i=0;i<arraySize;i++)
{
cout<<"Enter value for array element "<<i<<":";
cin>>arrayPtr[i];//reading values
}
void displayIntegerArray(int *arrayPtr,int arraySize)
{
for(int i=0;i<arraySize;i++)
cout<<&arrayPtr[i]<<": arrayPtr["<<i<<"] = "<<setw(15)<<arrayPtr[i]<<endl;
}
void findMaximumInteger(int *arrayPtr,int arraySize)
{
int maximum = arrayPtr[0];
for(int i=0;i<arraySize;i++)
{
if(maximum<arrayPtr[i])max=arrayPtr[i];
}
cout<<"Maximum integer in array is: "<<max<<endl;
}
int main()
{
int n;
//reading array size
cout<<"Enter desired array size:";
cin>>n;
int *a = new int[n];
cout<<"arrayPtr = "<<a<<endl;
populateIntegerArray(a,n);
displayIntegerArray(a,n);
findMaximumInteger(a,n);
cout<<"DELETING array at arrayPtr = "<<a<<endl;
delete a;
return 0;
}
First, you are missing the last } after the definition of populateIntegerArray and before the displayIntegerArray:
int populateIntegerArray(int *arrayPtr,int arraySize)
{
for(int i=0;i<arraySize;i++)
{
cout<<"Enter value for array element "<<i<<":";
cin>>arrayPtr[i];//reading values
}
void displayIntegerArray(int *arrayPtr,int arraySize)
Then, you have the following prototype:
int findMaximumInteger(int *arrayPtr, int arraySize);
But you define the actual function as returning void (it should be int instead):
void findMaximumInteger(int *arrayPtr,int arraySize)
{
Then, in your findMaximumInteger function, you are not defining the max variable anywhere.
Functions populateIntegerArray and findMaximumInteger are declared as returning int, but actually do not return a value. You should either return an integer, or (if you intend to just output the result to console without returning a value from a function) change the functions' prototypes to returning void (not int).
Related
So, I need to get the maximum value from an array. So the first input line contains the number of array elements, then the values for the array. But I am really confused as to why this error keeps popping up when I pass an integer type array to the function. I am finding it a little difficult to deal with pointers and functions at the moment.
#include<iostream>
using namespace std;
void maxim(long long int nums[],long long int n){
int max_val=0;
for(int i=0;i<n;i++){
if(nums[i]>max_val){
max_val=nums[i];
}
}
cout<<max_val<<endl;
}
int main() {
long long int n;
cin>>n;
long long int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
maxim(arr[n],n);
return 0;
}
#include<iostream>
using namespace std;
void maxim(long long int *nums,long long int n){
long long int max_val=nums[0];
for(int i=1;i<n;i++){
if(nums[i]>max_val){
max_val=nums[i];
}
}
cout<<max_val<<endl;
}
int main() {
long long int n;
long long int *arr;
cin>>n;
arr = new long long int[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
maxim(arr,n);
delete []arr;
return 0;
}
I wrote this simple code for bubble sort but it gives some random garbage values as output. Can someone please tell me my mistake. I tried to print the output of A[i] and A[j] in the function bubbleSort and looks like it is working fine. But why is the printSortedArray not giving the correct output? Thanks!
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void printSortedArray(int A[],int size)
{
cout<<"the sorted array is"<<endl;
int i;
for(i=0;i<size;i++);
{
cout<<A[i]<<" ";
}
}
void bubbleSort(int A[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if(A[j]>A[j+1])
{
swap(A[j],A[j+1]);
}
}
}
}
int main()
{
int A[50]; int size,i;
cout<<"enter the size of the array: ";
cin>>size;
cout<<"Enter the "<<size<<" numbers to be sorted"<<endl;
for(i=0;i<size;i++)
{
cin>>A[i];
}
bubbleSort(A,size);
printSortedArray(A,size);
return 0;
}
for(i=0;i<size;i++);
The trailing semicolon does not belong there. This results in undefined behavior.
The end result is that this function printed one garbage value after the end of the array.
I am using this code for the function overloading using C++ to sort a double and an integer array.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void sort(int arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted integer array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
void sort(double arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted double array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
int main()
{
int n;
cout<<"Enter the size of the array \n";
cin>>n;
cout<<"Enter the values for the integer function \n";
int a[n];
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}
double b[n];
cout<<"Enter the values for the double array \n";
for(i=0;i<n;i++)
{
cin>>b[i];
}
sort(a,n);
sort(b,n);
}
In the answer it rounds off the values of the double values except the first entry into the double array. Is there any type casting step which I am missing or any other flaw in the code?
You are irrevocably cutting off an floating part of double if you save it to int variable.
int key;
key=arr[j];
Thats why are you getting wrong results.
Declare key as double and it works.
Also VLAs (variable length arrays) are not standart C++.
Here are some other variants
std::array
std::vector
use new & delete
So I'm totally new to pointers, I apologize for this, I'm supposed to pass an array of pointers and get the mode of that array. After the array passes as a set of pointers, I can't manipulate the array to find the mode, everything I try results in a syntax error.
EDIT: I changed list to an array of pointers and I get a runtime error.
int main()
{
int size=0;
int *list[size];
cout<<"Please enter the size of your array: ";
cin>>size;
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>*list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
int getMode (int* list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int *number=list[0];
int count1=0;
int count2=0;
int mode=0;
for(int j=1;j<arraySize;j++)
{
for(int i=1;i<arraySize;i++)
{
if(list[i]==number)
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= *list[j];
count2=count1;
}
count1=0;
}
return mode;
}
When you pass an array to a function, it automatically decays to a pointer, so you don't need to use &list. And in the function, you shouldn't declare it int *list[], it should just be int list[] or int *list.
Also, in the getMode() function, you need to count the matches of list[j]. You're just counting the repetitions of number, which is list[0].
#include <iostream>
#include <iomanip>
using namespace std;
int getMode (int list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int count1=0;
int count2=0;
int mode=0;
for(int j=0;j<arraySize;j++)
{
for(int i=0;i<arraySize;i++)
{
if(list[i]==list[j])
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= list[j];
count2=count1;
}
count1=0;
}
return mode;
}
int main()
{
int size;
int *list;
cout<<"Please enter the size of your array: ";
cin>>size;
list=new int[size];
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
DEMO
I'm trying to solve a mystery that occured while doing the assignement.
The main and entry functions works just fine, but the average one doesnt. While checking if variables constant and arraysize, it seems that they do not to pass it's values like it should while I was dereferencing them.
And yes, i'm new to pointers, so any suggestions would be great.
Here's the code:
#include <iostream>
using namespace std;
void entry(int &size, int *arraysize, int &c, int *constant, int *firstArray, int *secondArray);
void average(int &size, int &c, int *arraysize, int *constant, int *firstArray, int *secondArray);
void newarray();
void output();
int main() {
int size;
int *arraysize;
int c;
int *constant;
int *firstArray;
int *secondArray;
entry(size, arraysize, c, constant, firstArray, secondArray);
average(size, c, arraysize, constant, firstArray, secondArray);
}
void entry(int &size, int *arraysize, int &c, int *constant, int *firstArray, int *secondArray) {
cout<<"Enter the size of the array: ";
cin>>size;
arraysize = &size;
cout<<"array size: "<<*arraysize<<endl;
cout<<"Enter the constant c: ";
cin>>c;
constant = &c;
cout<<"constant c size: "<<*constant<<endl;
firstArray = new int[*arraysize];
secondArray = new int [*arraysize];
for (int i=0; i<*arraysize; i++) {
cout<<"Enter the "<<i+1<<" element of the first row: ";
cin>>firstArray[i];
}
for (int i=0; i<*arraysize; i++) {
cout<<"Enter the "<<i+1<<" element of the second row: ";
cin>>secondArray[i];
}
}
void average(int &size, int &c, int *arraysize, int *constant, int *firstArray, int *secondArray) {
cout<<"Array size: "<<*arraysize<<endl;
cout<<"Constant: "<<*constant<<endl;
}
It show's me this kind of error, when program hits the average function
http://i.imgur.com/esF9c04.png
Modifying arguments in callee won't affect caller's local variables unless the arguments modified are reference.
Use reference as you did in int arguments.
Modify both prototype declaration and function definition like this:
void entry(int &size, int *&arraysize, int &c, int *&constant, int *&firstArray, int *&secondArray)
There is some errors in your code.
When you want to send values to a function/procedure and save their changes:
In C++ only:
For variables: When receiving int &variable and when sending send(variable)
For arrays: When receiving int *array and when sending send(array)
In C:
For variables: When receiving int *variable and when sending send(&variable)
For arrays: When receiving int *array and when sending send(array)