C++ Sum of prime numbers in matrix

In summary, the conversation discusses an attempt to create a program that sums up prime numbers in a matrix. The issue is that the program is not returning the correct sum and the conversation delves into the use of return statements and the main function in C++. The experts also provide guidance on properly using return statements and the importance of declaring int main instead of void main. The issue is eventually resolved and the correct output is achieved.
  • #1
Raghav Gupta
1,011
76

Homework Statement


My Program is not showing the sum value or not returning it. A blank space is coming.Why that is so?

Homework Equations


Showing the attempt below in form of code.

The Attempt at a Solution


Code:
#include<iostream.h>
#include<conio.h>
Prime_Sum(int arr[30][30],int m, int n);
void main()
{
    clrscr();
    int arr[30][30],i,j, m, n;
    cout<<"Enter the rows\n";
    cin>>m;
    cout<<"Enter the columns\n";
    cin>>n;
    cout<<"Enter the matrix elements\n";
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
     cin>>arr[i][j];
    cout<<"Matrix form:\n";
    for(i=0;i<m;i++)
    {
     for(j=0;j<n;j++)
      cout<<"\t"<<arr[i][j];
    cout<<"\n";
    }
    Prime_Sum(arr,m,n);
    getch();
}

Prime_Sum(int arr[30][30],int m,int n)
{
    cout<<"The sum of prime numbers in matrix is:\t";
    int sum=0,flag;
    for(int i=0;i<m;i++)
    {
    for(int j=0;j<n;j++)
    {
     for(int k=2;k<arr[i][j];k++)
     {
      if(arr[i][j]==2)
      {
       flag=1;
       break;
      }
     else if(arr[i][j]%k!=0)
     flag=1;
       else
       {
        flag=0;
        break;
       }
     }
      if(flag==1)
      sum+=arr[i][j];
    }
   }
    return sum; // not returning sum value, are brackets necessary? Although then also not returning.
}
The output is like this:
Enter rows
2
Enter columns
2
Enter elements
2
1
3
4
Matrix form:
2 1
3 4
The sum of prime nos. is:
and when I press key the screen terminates why that is so?
When I wrote void before Prime_Sum function and then instead of return sum wrote cout<< sum;
The output came. What is problem in return function here?
 
Last edited:
Physics news on Phys.org
  • #2
You haven't indicated the type the function should return.
 
  • #3
You have put everything on the screen by cout except 'Prime_Sum'. At the end 'Prime_Sum' has probably the value 'sum' as returned but you don't do anything with it.
 
  • #4
DrClaude said:
You haven't indicated the type the function should return.
fresh_42 said:
You have put everything on the screen by cout except 'Prime_Sum'. At the end 'Prime_Sum' has probably the value 'sum' as returned but you don't do anything with it.
Doen't return mean to print value.
I wrote int now as return type before Prime_Sum function in function prototype as well as before function definition yet on running no value of sum is been displayed.
 
  • #5
Raghav Gupta said:
Doen't return mean to print value.
I wrote int now as return type before Prime_Sum function in function prototype as well as before function definition yet on running no value of sum is been displayed.
"return" means that the value is returned to the point where the function was called. You need to review some basics of C/C++.
 
  • #6
DrClaude said:
"return" means that the value is returned to the point where the function was called. You need to review some basics of C/C++.
Okay so why then in int main()
{
...
...
return 0 ;}
return 0 is necessary. It will return 0 to function. How it means to exit. Otherwise also without using return the function would stop.
 
  • #7
Raghav Gupta said:
Okay so why then in int main()
{
...
...
return 0 ;}
return 0 is necessary. It will return 0 to function.
This value returned from main is returned to the operating system. In a unix shell, you can check that value to see if the program has terminated correctly. In case of an error, you can return a value different from 0. In addition, using for instance exit (-1) anywhere in the program will cause the program to terminate and return -1 to the operating system, which is now informed that there was an error.

This is also why you have to declare int main, never void main as you have done. Some compilers while let it pass, but strictly speaking, it is not allowed by the standard.

Raghav Gupta said:
Otherwise also without using return the function would stop.
I don't understand what you are saying.
 
  • #8
DrClaude said:
This value returned from main is returned to the operating system. In a unix shell, you can check that value to see if the program has terminated correctly. In case of an error, you can return a value different from 0. In addition, using for instance exit (-1) anywhere in the program will cause the program to terminate and return -1 to the operating system, which is now informed that there was an error.

This is also why you have to declare int main, never void main as you have done. Some compilers while let it pass, but strictly speaking, it is not allowed by the standard.I don't understand what you are saying.
No suppose we write
int main()
{
...
...
getch();}
then also the program executes and clicking on key terminates. Why return 0 to write if the program is running smoothly?
 
  • #9
Raghav Gupta said:
No suppose we write
int main()
{
...
...
getch();}
then also the program executes and clicking on key terminates. Why return 0 to write if the program is running smoothly?
By default, if main terminates without a return, it will return 0 to the operating system.
 
  • #10
DrClaude said:
By default, if main terminates without a return, it will return 0 to the operating system.
If we don't write return in called function then what happens by default in that case?
 
  • #11
DrClaude said:
By default, if main terminates without a return, it will return 0 to the operating system.
Sorry the above post I understood.
I am able to execute the program now. Thanks to both of you for guiding me and expalining the actual use of these things. I wrote now in main cout<<Prime_Sum(arguments);
And now the correct display is coming.
One thing I don't know here is that without declaring return type of Prime_Sum() how I am able to get correct output without error?
 
  • #12
Raghav Gupta said:
One thing I don't know here is that without declaring return type of Prime_Sum() how I am able to get correct output without error?
The compiler is probably typing the function as int by default. In any case, this behavior might not be portable: another compiler might give a different result. Always declare the type of your functions.
 
  • Like
Likes Raghav Gupta
  • #13
@Raghav Gupta, you have a lot of misconceptions.
The return keyword does NOT mean print a value. This keyword can be confusing, because it means two different things:
  1. Transfer control back to the function (or operating system) that called this function. A void function will typically have a final statement of return;
  2. Send a value back to the function (or operating system) that called this function. A function that has a return type needs to have a statement of return <value>; where <value> represents the expression that is to be returned.
Your Prime_Sum function is not declared correctly. Your prototype (or declaration) like this:
C:
Prime_Sum(int arr[30][30],int m, int n);
It should look lide this:
C:
int Prime_Sum(int arr[30][30],int m, int n);

Also, the header in your definition of this function should match the above, and look like this:
C:
int Prime_Sum(int arr[30][30],int m, int n)
{
   // rest of body of function
   ...
}

The Prime_Sum function should not have that output statement at the top. Instead, it should calculate the sum and return it to main().

As already noted, the header line for main should be int main( void )
Raghav Gupta said:
return sum; // not returning sum value, are brackets necessary? Although then also not returning.
I don't understand your question. Do you mean, does the above need to look like this: return (sum); ?
If so, no it doesn't. And BTW, these -- () -- are called parentheses, at least here in the US. These -- [] -- are brackets, and these -- {} -- are braces or "curly" braces.
 
  • Like
Likes Raghav Gupta
  • #14
Mark44 said:
The Prime_Sum function should not have that output statement at the top. Instead, it should calculate the sum and return it to main().
Why that is so if I am getting correct output?
 
  • #15
Raghav Gupta said:
Why that is so if I am getting correct output?
Because it's not the best programming style. Your Prime_Sum() function should use the information passed as parameters, do its calculation, and return the result, which can be displayed in main().
 
  • Like
Likes Raghav Gupta

FAQ: C++ Sum of prime numbers in matrix

How do I find the sum of prime numbers in a C++ matrix?

To find the sum of prime numbers in a C++ matrix, you will need to use a nested loop to iterate through each element in the matrix. For each element, you can check if it is a prime number using a function or algorithm. If it is a prime number, add it to a variable that keeps track of the sum. Once the loop has finished, the variable will contain the sum of all prime numbers in the matrix.

Can I use a pre-existing function to find the sum of prime numbers in a C++ matrix?

There is no built-in function in C++ specifically for finding the sum of prime numbers in a matrix. However, there may be libraries or external functions that can help with this task. It is also possible to create your own function to find the sum of prime numbers in a matrix.

How can I optimize the algorithm for finding the sum of prime numbers in a C++ matrix?

There are a few ways to optimize the algorithm for finding the sum of prime numbers in a C++ matrix. One approach is to only check numbers that are potential prime numbers, such as numbers that are not divisible by 2 or 3. Another approach is to use a more efficient prime number checking algorithm, such as the Sieve of Eratosthenes.

What is the time complexity of finding the sum of prime numbers in a C++ matrix?

The time complexity of finding the sum of prime numbers in a C++ matrix will depend on the approach and algorithm used. In general, it will be at least O(n^2) since you will need to iterate through each element in the matrix. However, using optimization techniques such as the ones mentioned in question 3 can improve the time complexity.

Can I find the sum of prime numbers in a non-square matrix using the same approach?

Yes, the same approach can be used to find the sum of prime numbers in a non-square matrix. You will still need to use a nested loop to iterate through each element, and check if it is a prime number. The only difference is that you will need to keep track of the dimensions of the matrix to ensure you are iterating through all elements.

Similar threads

Replies
4
Views
1K
Replies
3
Views
975
Replies
3
Views
969
Replies
7
Views
2K
Replies
2
Views
3K
Replies
2
Views
2K
Replies
8
Views
1K
Replies
3
Views
1K
Replies
10
Views
2K
Back
Top