C++ Sum of prime numbers in matrix

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue related to calculating the sum of prime numbers in a matrix. Participants are addressing problems with function return types, output display, and general programming practices. The scope includes technical explanations and debugging of code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports that their program is not displaying the sum of prime numbers and questions the return functionality of the Prime_Sum function.
  • Another participant points out that the return type of the Prime_Sum function has not been specified, which may lead to issues.
  • Some participants clarify that the return keyword does not mean to print a value but rather to send a value back to the calling function.
  • There is confusion about the necessity of return statements in functions and the implications of using void main versus int main.
  • One participant expresses that they received correct output without declaring a return type for Prime_Sum, leading to questions about compiler behavior.
  • Several participants emphasize the importance of declaring function return types for clarity and portability across different compilers.
  • There are discussions about programming style, suggesting that functions should perform calculations and return results rather than print output directly.

Areas of Agreement / Disagreement

Participants generally agree that the return type of functions should be declared, but there is disagreement on the necessity of certain return statements and the implications of using void main. The discussion remains unresolved regarding the best practices for function design and output handling.

Contextual Notes

Limitations include potential misunderstandings about the return keyword and function declarations, as well as varying interpretations of programming style and compiler behavior.

Raghav Gupta
Messages
1,010
Reaction score
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;
    count<<"Enter the rows\n";
    cin>>m;
    count<<"Enter the columns\n";
    cin>>n;
    count<<"Enter the matrix elements\n";
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
     cin>>arr[i][j];
    count<<"Matrix form:\n";
    for(i=0;i<m;i++)
    {
     for(j=0;j<n;j++)
      count<<"\t"<<arr[i][j];
    count<<"\n";
    }
    Prime_Sum(arr,m,n);
    getch();
}

Prime_Sum(int arr[30][30],int m,int n)
{
    count<<"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 count<< sum;
The output came. What is problem in return function here?
 
Last edited:
Physics news on Phys.org
You haven't indicated the type the function should return.
 
You have put everything on the screen by count except 'Prime_Sum'. At the end 'Prime_Sum' has probably the value 'sum' as returned but you don't do anything with it.
 
DrClaude said:
You haven't indicated the type the function should return.
fresh_42 said:
You have put everything on the screen by count 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.
 
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++.
 
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.
 
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.
 
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?
 
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 count<<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   Reactions: 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   Reactions: 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   Reactions: Raghav Gupta

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K