Solve this problem #1

We giving you ten integers in one line, both negative and positive numbers.
so your task is to retrieve the average of the negative and positive numbers separately.
You can use any programming language to complete this task.

Sample Inputs:
                                1 -22 -12 -3 4 76 56 -90 -8 10
                         

 

Comments

  1. #include

    int main(){
    int totP=0, totM=0,cP=0,cM=0,input;
    for(int i=0;i<10;i++){
    scanf("%d",&input);
    if(input>0){
    totP+=input; cP++;
    } else {
    totM+=input; cM++;
    }
    }
    printf("%d %d\n", totP/(cP>0?cP:1), totM/(cM>0?cM:1));
    return 0;
    }

    ReplyDelete
    Replies
    1. Your algo fails in this test case,
      1 2 3 1 4 -1 0 -8 0 -2

      haven't considered when the input is 0, 0 is neither positive nor negative..

      Delete
    2. Issue in problem statement. It has not stated anything about zero. My assumption was you get only positive and negative integers. :)

      Delete
  2. #include

    using namespace std;

    int main()
    {
    int n;
    int num;
    int ttla;
    int ttlb;

    cin>>n;

    for(int i=0;i<n;i++)
    {
    if(num<0){
    ttla =num+num;

    }
    else{
    ttlb = num+num;

    }


    }

    cout<<ttla<<endl;
    cout<<ttlb<<endl;
    }

    ReplyDelete
  3. /*
    @lang: C++ 11
    @author: ramith
    */
    #include
    double t1,t2=0;
    double n,n2=0;

    int main(){
    int temp;
    for(int i=0;i<10;i++){
    std::cin>>temp;
    temp==0?n2++: 1; //in case of 0
    temp>0?t1+=temp,n++ : t2+=temp; }

    std::cout<<t1/n<<" , "<<t2/(10.0-n-n2)<<std::endl; //prints average of positive , negative

    }

    ReplyDelete
  4. ncot=0
    ntot=0
    pcot=0
    ptot=0
    s='1 -22 -12 -3 4 76 56 -90 -8 10'
    slist=s.split ()
    for n in slist:
    if int (n)>0:
    ptot+=int (n)
    pcot+=1
    else:
    ntot+=int (n)
    ncot+=1
    print ('avg positive nos:',ptot/pcot)
    print ('avg negative nos:',ntot/ncot)

    ReplyDelete
  5. #include

    using namespace std;

    int main()
    {
    int x;
    int Nsum;
    int Psum;

    Nsum=0;
    Psum=0;

    for(int i=1; i<=10; i++){
    cin>>x;

    if(x<0){
    Nsum=Nsum+x;
    }
    else{
    Psum=Psum+x;
    }
    }
    cout<<Psum<<endl;
    cout<<Nsum<<endl;
    return 0;
    }

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. // Java Script

    var numberstring="1 -22 -12 -3 4 76 56 -90 -8 10";
    var totals=[];
    totals[0]=0;
    totals[1]=0;

    debugger;
    numberarray=numberstring.split(" ");
    for (i = 0; i < numberarray.length; i++) {
    if ( numberarray[i] >=0) {
    totals[0]+=parseFloat(numberarray[i]) ;
    }
    else{
    totals[1]+=parseFloat(numberarray[i]) ;
    }
    }

    console.log ("Positive Average" + totals[0]/numberarray.length);
    console.log ("Negative Average" + totals[1]/numberarray.length);

    Design for N numbers
    The test case of 0 (zero) considered as plus

    it is possible to convert numbers at the time of reading using array.Map(Number) - don't know which is faster

    Pl. Improve this code

    ReplyDelete

Post a Comment