1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Sum of prime numbers in matrix

  1. Nov 19, 2015 #1
    1. The problem statement, all variables and given/known data
    My Program is not showing the sum value or not returning it. A blank space is coming.Why that is so?

    2. Relevant equations
    Showing the attempt below in form of code.

    3. The attempt at a solution
    Code (C):

    #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: Nov 19, 2015
  2. jcsd
  3. Nov 19, 2015 #2

    DrClaude

    User Avatar

    Staff: Mentor

    You haven't indicated the type the function should return.
     
  4. Nov 19, 2015 #3

    fresh_42

    Staff: Mentor

    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.
     
  5. Nov 19, 2015 #4
    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.
     
  6. Nov 19, 2015 #5

    DrClaude

    User Avatar

    Staff: Mentor

    "return" means that the value is returned to the point where the function was called. You need to review some basics of C/C++.
     
  7. Nov 19, 2015 #6
    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.
     
  8. Nov 19, 2015 #7

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
  9. Nov 19, 2015 #8
    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?
     
  10. Nov 19, 2015 #9

    DrClaude

    User Avatar

    Staff: Mentor

    By default, if main terminates without a return, it will return 0 to the operating system.
     
  11. Nov 19, 2015 #10
    If we don't write return in called function then what happens by default in that case?
     
  12. Nov 19, 2015 #11
    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?
     
  13. Nov 19, 2015 #12

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
  14. Nov 19, 2015 #13

    Mark44

    Staff: Mentor

    @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:
    Code (C):
    Prime_Sum(int arr[30][30],int m, int n);
    It should look lide this:
    Code (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:
    Code (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 )
    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.
     
  15. Nov 19, 2015 #14
    Why that is so if I am getting correct output?
     
  16. Nov 19, 2015 #15

    Mark44

    Staff: Mentor

    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().
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C++ Sum of prime numbers in matrix
  1. Sums in brackets (Replies: 4)

Loading...