#include <iostream>
using namespace std;
int main()
{
const int max=26;
char c[max];
int i, j;
// Initialise array with letters of the alphabet (ascending order).
for(i=0; i<max; ++i )
c[i]='a'+i;
// Print array.
for(i=0; i<max; ++i)
cout<<c[i];
cout<<endl;
// Reverse array:
// work from both ends, swapping elements as we go,
// stopping when we reach the middle elements.
for(i=0, j=max-1; i<j; ++i, --j)
// Swap values without using a temporary variable.
c[i]^=c[j]^=c[i]^=c[j];
// Print array.
for(i=0; i<max; ++i)
cout<<c[i];
cout<<endl;
return(0);
}
Output:
abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
const int arraySize = 128;
char myString[arraySize] = {'0'};
cout << endl << "Enter a string: " << endl;
cin.getline(myString, 128, '\n');
int numberOfChars = 0;
while ((numberOfChars < (arraySize - 1)) && (myString[numberOfChars] != NULL))
{
++numberOfChars;
}
cout << endl << "You've entered:"
<< endl << myString << endl;
char myStringReversed[arraySize] = {'0'};
int index = 0;
while ((index <= numberOfChars) && (myString[index] != NULL))
{
myStringReversed[numberOfChars - index - 1] = myString[index];
++index;
}
cout << endl << "Reversed string is: "
<< endl << myStringReversed << endl;
system("PAUSE");
return 0;
}
Working from each end of the array to the middle element(s), swap the corresponding elements.
for (i=maxindex; i>=0; i--) printf("index: %d value: %d\n", i, array[i]);
Point to both ends of the array and swap the elements. Work your way towards the middle, swapping as you go. When the pointers meet or cross each other, you are done.
Point to both ends of the array. Work towards the middle swapping values as you go. When the pointers meet or cross each other, the array is completely reversed.
Using sorted(array,reverse=True)
The lowest subscript of an array in C, or C++ is 0.
Start by pointing to each end of the array. Work your way towards the middle of the array, swapping elements as you go. When the pointers meet or pass each other, the array is completely reversed.
#include main() { int array[100], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) { minimum = array[c]; location = c+1; } } printf("Minimum element is present at location number %d and it's value is %d.\n", location, minimum); return 0; }
Wright a 'C' program for storage representation of 2-D array.
public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i < array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }
A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.
Using sorted(array,reverse=True)
The lowest subscript of an array in C, or C++ is 0.
Heres something i whipped up in a hurry... This uses the Bubble Sort method found (related links) #include <iostream> using namespace std; int main(int argc, const char* argv) { int arraysize = 5; //Unsorted array size int array [] = { 5, 3, 4, 2, 1 }; //The array of numbers itself //Display the unsorted array cout << "Before: {"; for (int c=0; c <= arraysize; c++) { cout << array[c]; if (c != arraysize) { cout << ","; } } cout << "}" << endl; //Acctually sort the array int tmp=0; //Used for swaping values for (int loop=0; loop <= (arraysize - 1); loop++) { for (int c=0; c <= (arraysize - 1); c++) //The sort loop { if (array[c] > array[c + 1]) { //Swaps the two values in the array tmp = array[c]; array[c] = array[c + 1]; array[c + 1] = tmp; //Cleanup tmp = 0; } } } //Display the sorted array cout << "After: {"; for (int c=0; c <= arraysize; c++) { cout << array[c]; if (c != arraysize) { cout << ","; } } cout << "}" << endl; return 0; }
You cannot add elements to a fixed array in C or C++. If, however, the array is declared as a pointer to an array, you can add elements by allocating a new array, copying/adding elements as needed, reassigning the new array to the pointer, and deallocating the original array.
The syntax to access a particular element in an array are the same in both languages: For example: assume array is an array of 10 int(egers): to get the first element: array[0] (both are 0 based indexing] int i = 0; while (i < array.Length) { // do something to array[i] } int i = 0; int length = sizeof(array) / sizeof(int); while (i < length) { // do something to array[i] } However, an array in C# is also enumerable (C does not have this feature) in C#, you may loop thru the array by: foreach (int number in array) { // do something to array[i] } Plus, C# is an Object-Oriented Language, so that an array may be of some object types, not just those primitiives data types in C: object[] objectArray; // any object derived from Object may be placed into objectArray, not just struct. In another variation, an array may be of Delegate type in C#(sort of like function pointers in C)
Start by pointing to each end of the array. Work your way towards the middle of the array, swapping elements as you go. When the pointers meet or pass each other, the array is completely reversed.
If the array consists of r rows and c column, and the total number of cells in the array are n = r*c, then r*c = n and c*r = n so that r*c = c*r : which is commutativity of multiplication.
#include main() { int array[100], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) { minimum = array[c]; location = c+1; } } printf("Minimum element is present at location number %d and it's value is %d.\n", location, minimum); return 0; }
cod a program student degree array in c language
#include "stdio.h" #define ARRAY_SIZE 10 void fill(float* array, int size); void spill(float* array, int size, char* delimiter); void bubble_sort(float* array, int size); void reverse(float* array, int size); void swap(float* a, float* b); int main(int argc, char* argv[]) { float numbers[ARRAY_SIZE]; fill(numbers, ARRAY_SIZE); bubble_sort(numbers, ARRAY_SIZE); spill(numbers, ARRAY_SIZE, " "); reverse(numbers, ARRAY_SIZE); spill(numbers, ARRAY_SIZE, " "); return 0; } void fill(float* array, int size) { int i = 0; while (i < size) fscanf(stdin, "%f", array + (i++)); } void spill(float* array, int size, char* delimiter) { int i = 0; while (i < size) fprintf(stdout, "%f%s", array[i++], delimiter); fputc('\n', stdout); } void bubble_sort(float* array, int size) { int i, j; for (i = 0; i < size; i++) for (j = i; j < size; j++) if (array[i] > array[j]) swap(array + i, array + j); } void reverse(float* array, int size) { int i; for (i = size / 2; i >= 0; i--) swap(array + i, array + (size - (i + 1))); } void swap(float* a, float* b) { float c = *a; *a = *b; *b = c; } fill gets the numbers from input spill sends them to output bubble sort will sort the array in ascending order reverse will reverse the list so that it is in descending order swap is used to swap two floats You can change float to double or int depending on which datatype you want to use.