Question:
print 5 numbers using array?
anonymous
2014-10-30 19:40:30 UTC
print 5 numbers using array?
Four answers:
Jamie
2014-10-30 20:09:36 UTC
If you would like to print five numbers from an array, one after another, you can do this by using a "for loop" to print the numbers to the screen, assuming they were already stored in the array.



Here's what some Java code would look like to print the numbers one to five, stored in an array with five indices.



public class PrintArray{



public static void main(String [] args){



//create a new array of type "int" with five indices (0-4) to hold five integers

int[] intArray = new int[5];



//manually add some values to the array (this can be done using a "for loop", as well)

intArray[0] = 1;

intArray[1] = 2;

intArray[2] = 3;

intArray[3] = 4;

intArray[4] = 5;



//Now that we have added five integers to our array, we can print it using a "for loop", rather than

//having to write five separate "System.out.println()" statements

for(int i = 0; i <= 4; i++){



//print out the value stored in the "ith" index of the "intArray" array

System.out.println(intArray[i]);



}



}



}



Alternatively, here's a link to the above code on PasteBin.com, which can be viewed with proper formatting and syntax highlighting.



http://pastebin.com/JHnQdtUg



One thing to note, however, is that if you are required to get the numbers to be stored in the array from a user, you could do this using the "Scanner" class, which is in the "java.util" library.



Best of luck and I hope I helped you!
anonymous
2014-10-31 05:36:12 UTC
#include

#include

#define MAX 30



void main() {

int size, i, arr[MAX];

int *ptr;

clrscr();



ptr = &arr[0];



printf("\nEnter the size of array : ");

scanf("%d", &size);



printf("\nEnter %d integers into array: ", size);

for (i = 0; i < size; i++) {

scanf("%d", ptr);

ptr++;

}



ptr = &arr[size - 1];



printf("\nElements of array in reverse order are :");



for (i = size - 1; i >= 0; i--) {

printf("\nElement%d is %d : ", i, *ptr);

ptr--;

}



getch();

}

Output :



Enter the size of array : 5

Enter 5 integers into array : 11 22 33 44 55

Elements of array in reverse order are :

Element 4 is : 55

Element 4 is : 44

Element 4 is : 33

Element 4 is : 22

Element 4 is : 11
no1home2day
2014-10-30 19:43:27 UTC
Okay, I did it. Now YOU do it and earn an honest grade for your class.
?
2014-10-30 19:42:39 UTC
In what language? Matlab? C? C++? C#? Please specify :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...