C++ Sum of prime numbers in matrix

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
14 replies · 4K views
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:
on Phys.org
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.
 
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?
 
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?
 
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
@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
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?
 
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