/* I'm using four spaces instead of tab because I'm writing this on an iPhone. */ #include #include int main() { int num1, num2, num3, num4, sum; printf("Please enter first number: "); scanf("%d", num1); printf("Please enter second number: "); scanf("%d", num2); printf("Please enter third number: "); scanf("%d", num3); printf("Please enter fourth number: "); scanf("%d", num4); sum = pow(num1, 2) + pow(num2, 2) + pow(num3, 2) + pow(num4, 2); printf("The answer is %d", sum); return 0; }
1. Read the 3 nos a,b,c 2. Let larget = a 3. if b > largest then largest = b 4. if c > largest then largest = c..... If you have to process more nos, read all of them in an array. Assume the first element be largest, do comparison through all elements of the array.... Similar algorithm can be developed for finding the lowest also. /*lab practice 2 damithguruge question 4 */ #include<stdio.h> int main() { int num1,num2,num3,num4; int smallest; printf("Please enter a number1"); scanf("%d%*c", &num1); printf("Please enter a number2"); scanf("%d%*c" ,&num2); printf("Please enter a number3"); scanf("%d%*c", &num3); Printf("Please enter a numbe4r"); scanf("%d%*c", &num4); /* num1 set as the smallest */ smallest=num1; if(smallest > num2) then smallest=num2; else if smallest >num3 then smallest=num3; else if smallest>num4 then smallest=num4; printf("smallest number:%d\n,smallest"); return(0); endif endif endif }
The cheap way out, just use what java has available: the Arrays class (of course you have to import it first). int[] array = {42, 12, 1337, 0}; // the four numbers to be sorted Arrays.sort(array); //Now array is sorted // array should now contain {0, 12, 42, 1337}; Of course there are other ways to sort, especially writing algorithms yourself, but that's such a bore. Just for reference I have attached links to common sorts below.
Byte StreamsIf a program needs to read/write bytes (8-bit data), it could use one of the subclasses of the InputStream or OutputStream respectively. The example below shows how to use the class FileInputStream to read a file named abc.dat. This code snippet prints each byte's value separated with white spaces. Byte values are represented by integers from 0 to 255, and if the read() method returns -1, this indicates the end of the stream.import java.io.FileInputStream;import java.io.IOException;public class ReadingBytes {public static void main(String[] args) { FileInputStream myFile = null; try {myFile = new FileInputStream("c:\\abc.dat"); // open the streamboolean eof = false;while (!eof) {int byteValue = myFile.read(); // read the streamSystem.out.println(byteValue);if (byteValue == -1){eof = true;}//myFile.close(); // do not do it here!!!}}catch (IOException e) {System.out.println("Could not read file: " + e.toString());} finally{try{if (myFile!=null){myFile.close(); // close the stream}} catch (Exception e1){e1.printStackTrace();}}}}Please note that the stream is closed in the clause finally. Do not call the method close() inside of the try/catch block right after the file reading is done. In case of exception during the file read, the program would jump over the close() statement and the stream would never be closed!The next code fragment writes into the file xyz.dat using the class FileOutputStream:int somedata[]={56,230,123,43,11,37};FileOutputStream myFile = null;try {myFile = new FileOutputStream("c:\xyz.dat");for (int i = 0; i BufferedInputStream --> DataInputStreamFileInputStream myFile = new FileInputStream("myData.dat");BufferedInputStream buff = new BufferedInputStream(myFile);DataInputStream data = new DataInputStream(buff);try {int num1 = data.readInt();int num2 = data.readInt();float num2 = data.readFloat();float num3 = data.readFloat();float num4 = data.readFloat();double num5 = data.readDouble();} catch (EOFException eof) {...}