This question already has answers here:
How to return an array from a function?
(5 answers)
Closed 7 years ago.
I try to return an array from function in C++.
I made a very easy function to demonstrate it.
#include<iostream>
using namespace std;
int OneDimensional();
void main()
{
int arr[3];
arr = OneDimensional();
cout<<"arr = " << arr[0] <<endl;
cin.get(); cin.get();
}
int OneDimensional()
{
int arr[3];
cout << "Enter a number" <<endl;
cin >> arr[0];
cout << "Enter a number" <<endl;
cin >> arr[1];
cout << "Enter a number" <<endl;
cin >> arr[2];
return arr;
}
But it fails with a lot of errors.
you need to use a pointer.
int * OneDimensional()
{
static int arr[3];
cout << "Enter a number" <<endl;
cin >> arr[0];
cout << "Enter a number" <<endl;
cin >> arr[1];
cout << "Enter a number" <<endl;
cin >> arr[2];
return arr;
}
void main()
{
int *arr;
arr = OneDimensional();
for (int i = 0; i < length; i++ )// length is number of elements in array
{
cout<<"arr = "<< *(arr + i) << endl;
}
}
Check the example here
int OneDimensional() return only a int value, not an array.
main() doesn't have a return
You need read more about C or C++.
Goold Luck!
Related
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Variable length arrays (VLA) in C and C++
(5 answers)
Closed 6 months ago.
What does " control reaches end of non-void function" means??
How to remove the warnings from out code ?
#include <bits/stdc++.h>
using namespace std;
int LinearSearch()
{
int ans=-1;
cout << "Enter the Size of the array: \n";
int n;
cin >> n;
cout << "Enter the array elements: \n";
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter the key: \n";
cin >> key;
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
cout << "the " << key << " is found at index " << i << endl;
}
return ans;
}
}
int main()
{
while (1)
{
cout << "\t Main Menu\n";
cout << "1. For Linear Search\n";
cout << "2. For Binary Search\n";
cout << "3. For First and last Occurence\n";
int ch;
cout << "Enter the choice: \n";
cin >> ch;
switch (ch)
{
case 1:
cout<< LinearSearch();
break;
case 2:
break;
case 3:
break;
default:
cout << "Invalid Choice OOps!! ";
break;
}
}
return 0;
}
enter image description here
AS I am trying to run it it is giving me Warning Why??
Error is: warning: control reaches end of non-void function [-Wreturn-type]
How to resolve it?
When n is zero, you never reach a return. You need a return after the loop. Returning -1 might be the sensible choice in that situation. (Also, the return in the loop is misplaced...)
It means that int LinearSearch() is expected to return an int but there are code paths where that does not happen. For instance, if n == 0. You fix this by adding a return statement with an appropriate value on that code path. It's probably an error that you have the return within the for() loop as this means you get at most one iteration. Maybe this is what you want?
int LinearSearch()
{
int ans=-1;
cout << "Enter the Size of the array: \n";
int n;
cin >> n;
cout << "Enter the array elements: \n";
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter the key: \n";
cin >> key;
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
cout << "the " << key << " is found at index " << i << endl;
ans = i;
break;
}
}
return ans;
}
Like others have mentioned, the warning indicates one of your code path has no return value. In LinearSearch after the for loop a return is missing. You can use -1 to return when a key match is not found or better still is if your C++ compiler supports C++17 or higher standard then I would suggest using std::optional and to return a "no value" to use std::nullopt and return the "ans" when you actually find the key.
Please look at the code below for a sample implementation.
#include <optional>
using namespace std;
std::optional<int> LinearSearch()
{
int ans;
cout << "Enter the Size of the array: \n";
int n;
cin >> n;
cout << "Enter the array elements: \n";
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter the key: \n";
cin >> key;
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
cout << "the " << key << " is found at index " << i << endl;
return ans;
}
}
return std::nullopt;
}
int main()
{
while (1)
{
cout << "\t Main Menu\n";
cout << "1. For Linear Search\n";
cout << "2. For Binary Search\n";
cout << "3. For First and last Occurence\n";
int ch;
cout << "Enter the choice: \n";
cin >> ch;
switch (ch)
{
case 1:{
auto res = LinearSearch();
if (res) cout<< *res;
else cout << "key not found";
}
break;
case 2:
break;
case 3:
break;
default:
cout << "Invalid Choice OOps!! ";
break;
}
}
return 0;
}
Program:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Structure
struct Student{
string name;
int pMark;
int eMark;
double avg;
string result;
};
//Functions
Student info(int arrSize, Student arrStudent[10]);
void display(int arrSize, Student arrStudents[10]);
//Main program
int main() {
Student arrStudents[10];
int arrSize;
cout << "How many Students are there(max 10): ";
cin >> arrSize;
info(arrSize, arrStudents);
display(arrSize, arrStudents);
return 0;
}
//Student Function
Student info(int arrSize, Student arrStudent[10]){
int counter = 0;
while(counter < arrSize){
cout << "\nStudent " << (counter + 1) << " info:";
cout << "\nEnter the name: ";
cin >> arrStudent[counter].name;
cout << "Enter the participation mark: ";
cin >> arrStudent[counter].pMark;
cout << "Enter the exam mark: ";
cin >> arrStudent[counter].eMark;
arrStudent[counter].avg = (arrStudent[counter].pMark + arrStudent[counter].eMark) / 2.00;
if (arrStudent[counter].avg >= 50) {
arrStudent[counter].result = "Pass";
}
else {
arrStudent[counter].result = "Fail";
}
counter++;
}
return arrStudent;//(Return Array)?
}
//Display Function
void display(int arrSize, Student arrStudents[10]) {
cout << endl << "Name\t\t Average\t\t Result" << endl;
for (int counter = 0; counter < arrSize; counter++) {
cout << arrStudents[counter].name << "\t\t"
<< fixed << setprecision(2)
<< arrStudents[counter].avg << "\t\t\t"
<< arrStudents[counter].result << endl;
}
}
I tried using the function as such, but I'm not sure if it's correct?
//Student Function
Student info(int arrSize, Student arrStudent[10]){
int counter = 0;
while (counter < arrSize) {
cout << "\nStudent " << (counter + 1) << " info:";
cout << "\nEnter the name: ";
cin >> arrStudent[counter].name;
cout << "Enter the participation mark: ";
cin >> arrStudent[counter].pMark;
cout << "Enter the exam mark: ";
cin >> arrStudent[counter].eMark;
arrStudent[counter].avg = (arrStudent[counter].pMark + arrStudent[counter].eMark) / 2.00;
if (arrStudent[counter].avg >= 50) {
arrStudent[counter].result = "Pass";
}
else {
arrStudent[counter].result = "Fail";
}
counter++;
}
return arrStudent[arrSize];
}
I'm new to coding(In University) so we still need to learn about vectors, pointers and references. That's why I haven't tried any other methods. I would highly appreciate the solutions if it is possible to solve it by avoiding those methods.
You are passing values of array by value, you should pass them by referance, thus you should use pointer,
Example function:
Student info(int arrSize, int *arrStudent)
I need to input how many authors/rows there will be but with constant value it's impossible
In other words, this
const int n = 2;
struct books b[n];
int i;
must be changed to something like this
int n;
cin >> n;
struct books b[n];
I think the solution has something to do with dynamic allocation but I don't know exactly how to realize it
Full code:
#include <cstring>
#include <iostream>
using namespace std;
struct books
{
string name;
string nameOfBook;
string publisher;
int year;
};
int main() {
const int n = 2;
struct books b[n];
int i;
int n;
cin >> n;
struct books b[n];
for (i = 0; i < n; i++)
{
cout << "Author " << i + 1 << endl;
cout << "Enter name" << endl;
cin >> b[i].name;
cout << "Enter a name of a book" << endl;
cin >> b[i].nameOfBook;
cout << "Enter a publisher" << endl;
cin >> b[i].publisher;
cout << "Enter the year of publishing" << endl;
cin >> b[i].year;
cout << endl;
}
cout << "Author \t" << "Name of an author: \t" << "Name of a book: \t" << "Name of a publisher: \t" << "The year of publishing: \t" << endl;
for (i = 0; i < n; i++)
{
cout << i + 1 << "\t" << b[i].name << "\t\t\t" << b[i].nameOfBook << "\t\t\t" << b[i].publisher << "\t\t\t" << b[i].year << endl;
}
return 0;
}
What you want is an array that can be resized at runtime, which is known as a dynamic array. struct books b[n]; is a static array, meaning that it is resolved at compile time. So what you are looking for is std::vector<books> b(n).
Secondly you have some variables with the same name,
const int n = 2; // #1
struct books b[n]; // #2
int i;
int n; // <-- Redefinition of #1.
cin >> n;
struct books b[n]; // <-- Redefinition of #2.
You cannot have redefinitions in the same scope. So make sure that all your variables in a scope has different names.
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 5 years ago.
Whenever i write 5 as n and p as 2 ,i get the output as 24...please let me know what's wrong? for other numbers it is completely fine.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
double n, p;
cout << "enter the number" <<endl;
cin >> n;
cout << "enter the power" <<endl;
cin >> p;
int result = pow(n, p);
cout << "Result is " << result;
return 0;
}
You have problems with your data types!
double n, p; // here you use double types
std::cout << "enter the number" << std::endl;
std::cin >> n;
std::cout << "enter the power" << std::endl;
std::cin >> p;
int result = pow(n, p); // then here you use double pow(double, double) but assign it to an int
std::cout << "Result is " << std::result;
Solution:
double result = pow(n, p);
#include<iostream>
using namespace std;
void arrayin(int x[], int n);
void arrayout(int x[], int n);
main()
{
int n, x[n];
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
cout << "Please enter the elements: " << endl;
arrayin(x,n);
cout << "Array is of " << n << " elements."<< endl;
cout << "Elements are as follow :" << endl;
arrayout(x,n);
}
void arrayin(int x[],int n)
{
for (int i = 0; i < n; i ++)
{
cin >> x[i];
}
}
void arrayout(int x[], int n)
{
for (int i = 0; i < n; i++)
{
cout << x[i] << "\t";
}
}
I'm new to programming.
It crashes for more than 8 elements, if n > 8 crashes.. but for n<8 works fine..
Dont know why!
Here is the problem:
int n, x[n]; // It is undefined behaviour
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
The correct way is (on your compiler with the variable-size-array extension):
int n;
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
int x[n];
The correct way using C++ is to use std::vector instead:
int n;
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
std::vector<int> x(n);
and you have to make some other changes to adapt std::vector.
The problem is here:
int n, x[n]; // <-- n is not yet initialized
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
cout << "Please enter the elements: " << endl;
arrayin(x,n);
You need this:
int n;
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
int x[n]; // << now n has been initialized
cout << "Please enter the elements: " << endl;
arrayin(x,n);
BTW: VLA (or dynamic arrays as you call them) are non standard in C++, but gcc (and possibily clang) has them as extension.
int n, x[n]; is the problem
You are declaring n that will have an indeterminate value. With this value you are declaring an array that will have an indeterminate size.
You are using C++, so use new keyword to create your array, after user input:
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
int *x = new int[n];
// your stuff
delete x;
Declare your array after taking input for n.
#include<iostream>
using namespace std;
void arrayin(int x[], int n);
void arrayout(int x[], int n);
main()
{
int n;
cout << "Please enter the number of elements in the array: " << endl;
cin >> n;
int x[n];
cout << "Please enter the elements: " << endl;
arrayin(x,n);
cout << "Array is of " << n << " elements."<< endl;
cout << "Elements are as follow :" << endl;
arrayout(x,n);
}
void arrayin(int x[],int n)
{
for (int i = 0; i < n; i ++)
{
cin >> x[i];
}
}
void arrayout(int x[], int n)
{
for (int i = 0; i < n; i++)
{
cout << x[i] << "\t";
}
}
This code works. The problem is when you declare array with no value for n the array will be initialized with whatever is on stack from past. Variable length arrays require the value. So declare the array later.
Someone downvoted without any reason.
Edit: Variable length arrays are not part of ISO C++. To write standard compliant C++ code you should use -pedantic flag with g++ or clang.
You have two simple choice. Either use std::array for fixed length arrays or std::vector for dynamic arrays.