The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please help with the following fork code..with complete explanation prakashabii Homework & Coursework Questions 1 4 Weeks Ago 06:31 PM
A complete New Bie Here supercops UNIX for Dummies Questions & Answers 5 01-28-2009 12:00 PM
BASH complete-filename & menu-complete together Mithu UNIX for Dummies Questions & Answers 0 01-06-2009 03:00 PM
how i prepare a c++ code(c code) for implementing my own protocol format amitpansuria High Level Programming 1 09-06-2007 11:09 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
HardyV2 HardyV2 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 11
Help me complete my code.

Ok, so I have done the 'mathematical part' just I am not sure where to put them in and how to get them to return answers.

Code:
public class Main {

    public static void main(String[] args) {

        
    }
//Part A.

//1&2- Computes the sum andproduct of all of the elements of the array a.
        public function SumAndProduct()
        {
            var arr:Array = [1, 2, 3, 4, 5];
            var sum:int = 0;
            var prod:int = 1;
 
            for (var i:int = 0; i < arr.length; i++)
            {
                sum += arr[i];
                prod *= arr[i];
            }
 
            trace("Sum: " + sum); // 15
            trace("Product: " + prod); // 120
        }
    }

//4- Finds the maximum value of the elements of the array.
var x=new Array(3,4,5,6);
var j=0;
for(i=0;i<x.length;i++)
  {
  if(x[i]>x[j])
  j=i;
  }
alert(x[j]);

//5- Computes the average value of the array elements.

    double nums[]={1.0,2.3,3.4,4.5,40.5};
    double result=0.0;
    
    for(i=0; i < nums.length; i++){
      result = result + nums[i];
    }
    System.out.println("Average is =" + result/nums.length);
    }

//Part B.

//1- Adding and subtracting each successive term.
 public static int alterningSum (int[] a){
       
       for (int i = 0; i < a.length; i++){
           p += a[i];
           i++;
       }
       for (int j = 1; j < a.length; j++){
           m -= a[j];
           j++;
       }
       
       
    }

//2- Returns the variance of the elements in the array.
    public static double variance(double[] a){
        double avg = average(a);
        double acc = 0.0;
        double var = 0.0;
        for (int i = 0; i < a.length; i++ ){
            acc += Math.pow (( a[i] - avg ), 2.0);
        }
        var = acc / (double) a.length;
        return var;
    }


//3- Returns the number that occurs most frequently.
    public static int mode(int[] a){
    int[] freq = frequence(a);
    int l = 0;
    int m = 0;
    int max = freq[0];

    for (int i = 0; i < freq.length; i++){
        if (freq[i] > max){
            max = freq[i];
            l = i;
        }
 
    }
 
    }

//Part C.

//1- Scales each element of the array by multiplying it by factor.
    public static void scale(int[]a, int factor){
        for (int i = 0; i < a.length; i++){
            a[i] *= factor;
        }

    }

//2- Exchanges the elements a[i] and a[j].
     public static void swap(int[] a, int i, int j){
        System.out.println("Swap array :");
        if ( i < ( a.length - 1) && j < ( a.length - 1 ) ){
            
            a[i] = a[j];
            a[j] = a[i];
             
        }
       
     }

//3- Replaces each element i of the array with the cumulative sum of the elements a[0], a[1], ... , a[i].
      public static void cumulate(int[] a){
        System.out.println("Cumulate array :");
        int temp = 0;
        for (int i = 0; i < a.length; i++){
            temp += a[i];
            a[i] = temp;
            
        }
        
      }




//Part E.

//1- Returns an array containing the first n squares starting from 0.
public static int[] squares(int n){
       int[] res = new int[n + 1];

       for (int k = 0; k < res.length; k++){
            res [k] = k;
            res[k] *= res[k];
        }
       return res;
 }
I'd appreciate if someone could help me with:

double maxProfit(double[] prices)

Suppose that the array prices contains the daily share price for a number of days. This method calculates the maximum possible profit that you could have made on this share if you had bought it on one of the days, and sold it on one of the later days.
For example, suppose the prices are {10.80, 11.00, 10.50, 12.00, 14.00, 13.00}. Then if you have bought on day 0 and sold on day 5, you would have made $2.20. But if you bought on day 2 and sold on day 4 you would have made $3.50.

Not sure how to do this one, but I think that there's a need of loops.

(New to java, but know some knowledge from perl which helped me here)

~H
  #2 (permalink)  
Old 4 Weeks Ago
HeavyJ HeavyJ is offline
Registered User
  
 

Join Date: Oct 2009
Location: Toronto
Posts: 18
Hello,

Why Java? If you need a GUI or web site integration you might be on the right track.

I prefer standard C due its ability to introduce pseudo classes with law enforcement taken care of by the programmer, as well as the ability to use extreme low level optimizations.

And your subject line on this post is a huge red flag.

All the Best,

Heavy J

Last edited by HeavyJ; 4 Weeks Ago at 08:10 PM.. Reason: typo
  #3 (permalink)  
Old 4 Weeks Ago
HardyV2 HardyV2 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 11
Huge red flag?

We are allowed to post school work after we've tried it ourselves I believe... And clearly I have, I just don't know how to get each code working.

Do I put them in the Main class? Or in the public static void main(String[] args)? and how do I get them to return values? I am not asking that you make the code for me, since I've already done...

~H
  #4 (permalink)  
Old 4 Weeks Ago
HeavyJ HeavyJ is offline
Registered User
  
 

Join Date: Oct 2009
Location: Toronto
Posts: 18
HardyV2,

School work, all right... That answers the "why Java?" question.

Red Flag = way too general = not that important = don't answer me

I think you should ask your teacher or prof. about the class structure.

When using extreme optimization, a programmer decides where everything should go, so that it will run as fast as computer can run it. Classes are a level of abstraction that come in handy when writing programs 50,000 lines long, otherwise they are just data structures.

In conclusion, I doubt that anyone else will look at this post because the subject line is extremely weak, and I only looked at it because I have been unemployed after getting a degree in Aerospace Engineering in 2005.

Ask your teacher... Or look in your text book.
  #5 (permalink)  
Old 4 Weeks Ago
HardyV2 HardyV2 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 11
KK, thxz anyways for the time.

~H
  #6 (permalink)  
Old 4 Weeks Ago
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,839
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0