#inculde<iostream>
using namespace std;
int smallestIndex(int[],int) //function prototype
void main()
{
int arr[10]={2,5,6,9,3,7,1,15,12,10};
int position;
position=smallestIndex(arr,10);
cout<<"the smallestIndex
#include <stdio.h> int main() { int number, temp; printf("enter the number"); scanf("%d",&number); printf("\n reverse of %d",number); while(number>0) { temp=temp*10+number%10; number=number/10; } printf(" is %d",temp); }
element_address = array_address + (element_index * element_size) Note that the array name refers to the start address of the array, element indices are always zero-based and element size is calculated at compile time using the sizeof() operator using the array type as the operand. int x[10]; int* p1 = x + (5 * sizeof(int)); // calculate address of 6th element int* p2 = &x[5]; // return address of 6th element assert (p1==p2); // ensure both address are equal Note that the array suffix operator [] used by p2 is merely sugar-coating. Behind the scenes, the compiler will automatically generate code equivalent to that of the p1 calculation. As such, there is no advantage in using one over the other.
An array is a contiguous block of memory containing one or more elements of the same type and size. Each element in the array is accessed as a zero-based offset from the start of the array or by using the index []. Examples: int a[10]; // allocates memory for 10 integers (e.g., 40 bytes for a 4 byte int). int x = a[5]; // accesses the 6th element (a[0] is the first element). int *p = a; // point to start of the array. p += 5; // advance pointer 5 * sizeof( int ) addresses. *p = 10; // access the 6th element and assign the value 10. int d * = new int[*p]; // dynamically allocate an array of 10 elements. delete [] d; // release dynamic array.
int array[10] = {...}; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }
#include<stdio.h> #include<conio.h> int main(void) { int a[10],i;//array declaration clrscr(); printf("\n enter the elements of array"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("\n the elements you enter into the array"); for(i=0;i<10;i++) printf("%5d",a[i]); getch(); return 0; }
use a loop(for or while) to get the input from the user then store each in the array using the Scanner class like this import java.util.Scanner;//before class declaration int[] Array = new int[10]; //just an example gets 10 ints from user Scanner input = new Scanner(System.in); then something like this for(int i = 0; i < 10;i++) { System.out.println("enter number:"); Array[i]= input.nextInt(); }
There are a number of different ways to loop using PHP. They're as follows:ForForeachWhileDo-whileBelow are some examples of how each of these types of loop work.ForForeachWhileDo-while
#include <stdio.h> int main() { int number, temp; printf("enter the number"); scanf("%d",&number); printf("\n reverse of %d",number); while(number>0) { temp=temp*10+number%10; number=number/10; } printf(" is %d",temp); }
element_address = array_address + (element_index * element_size) Note that the array name refers to the start address of the array, element indices are always zero-based and element size is calculated at compile time using the sizeof() operator using the array type as the operand. int x[10]; int* p1 = x + (5 * sizeof(int)); // calculate address of 6th element int* p2 = &x[5]; // return address of 6th element assert (p1==p2); // ensure both address are equal Note that the array suffix operator [] used by p2 is merely sugar-coating. Behind the scenes, the compiler will automatically generate code equivalent to that of the p1 calculation. As such, there is no advantage in using one over the other.
An array is a contiguous block of memory containing one or more elements of the same type and size. Each element in the array is accessed as a zero-based offset from the start of the array or by using the index []. Examples: int a[10]; // allocates memory for 10 integers (e.g., 40 bytes for a 4 byte int). int x = a[5]; // accesses the 6th element (a[0] is the first element). int *p = a; // point to start of the array. p += 5; // advance pointer 5 * sizeof( int ) addresses. *p = 10; // access the 6th element and assign the value 10. int d * = new int[*p]; // dynamically allocate an array of 10 elements. delete [] d; // release dynamic array.
int array[10] = {...}; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }
#include<stdio.h> #include<conio.h> int main(void) { int a[10],i;//array declaration clrscr(); printf("\n enter the elements of array"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("\n the elements you enter into the array"); for(i=0;i<10;i++) printf("%5d",a[i]); getch(); return 0; }
To count the number of times a digit occurs in an integer, start by initializing an array of ten counts of digits, such as int digits[10];Then, in a loop while the number is non zero, increment the element in the digits array that corresponds to the units digit, and then divide the number by ten, such as digits[number%10]++ and number/=10;int digits[10];int i;int number = some number;for (i=0; i
calculate 10% of number ($2950.00 x 10% = 295.00) then divide it by 12.
There are multiple answers to this question, but the most basic one common to both Java and C is the array. An array is a simple structure that you initialize to a certain size and fill with data. Think of an array as a sort of list, where each element in the list is numbered starting from zero up to the list size minus one (or the array is zero-based, as it's also called). In Java: // 10 is the number of elements the array can hold int[] myIntArray = new int[10]; myIntArray[0] = 2; // The first element in the array myIntArray[9] = 4; // The last element in the array Referencing myIntArray[10] or higher will cause a runtime error in Java, which may stop your program. In C: // 10 is the number of elements the array can hold int myIntArray[10]; myIntArray[0] = 2; // The first element in the array myIntArray[9] = 4; // The last element in the array Referencing myIntArray[10] or higher results in a buffer overflow in C (and C++). Note that in C, this won't throw errors like they do in Java, and this can and very likely will cause your program to have random bugs and possibly even crash from a segmentation fault, so be a bit more careful about using arrays in C.
#include<stdio.h> #include<conio.h> int arr(int a[]); void main() { int a[10],i; clrscr(); printf("Enter the value for array") for(i=0;i<10;i++) { scanf("%d",&a[i]); } arr(a); getch(); } int arr(int a[10]) { int i; printf("Elements of array\n"); for(i=0;i<10;i++) { printf("\n%d",a[i]); } return 0; }
to create a new Java array use typeName[] arrayName = new typeName[10]; This gives an array with 10 elements. To set the elements you can use arrayName[index] = value; Remember that the index number starts at 0, so the array will only go to index 9. You can also declare the contents when the array is created typeName[] arrayName = {value1, vaue2, ...} The values used in the array must be objects. In java 5+ you can use primitive types with no concern due to auto-boxing.