0% found this document useful (0 votes)
7 views

Arrays Unidos

Uploaded by

hernan23fdr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arrays Unidos

Uploaded by

hernan23fdr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 18

#include <stdio.

h>

#define ARRAY_SIZE 5

void main(void)
{
int values[ARRAY_SIZE] = {80, 70, 90, 85, 80};
int i;

for (i = 0; i < ARRAY_SIZE; i++)


printf("values[%d] %d\n", i, values[i]);
}
#include <stdio.h>

void main(void)
{
int values[5] = {80, 70, 90, 85, 80};
int i;

for (i = 0; i < 5; i++)


printf("values[%d] %d\n", i, values[i]);
}
#include <stdio.h>

void main(void)
{
int count = 1;
float salary = 40000.0;
long distance = 1234567L;

printf("Address of count is %x\n", &count);


printf("Address of salary is %x\n", &salary);
printf("Address of distance is %x\n", &distance);
}

#include <stdio.h>

void main(void)
{
int count[10];
float salaries[5];
long distances[10];

printf("Address of the array count is %x\n", count);


printf("Address of the array salaries is %x\n", salaries);
printf("Address of the array distances is %x\n", distances);
}
#include <stdio.h>

void main(void)
{
int scores[100];
float salaries[100];
char string[100];

printf("Bytes used to hold int scores[100] is %d bytes\n",


sizeof(scores));

printf("Bytes used to hold int salaries[100] is %d bytes\n",


sizeof(salaries));

printf("Bytes used to hold char string[100] is %d bytes\n",


sizeof(string));
}

#include <stdio.h>

void main(void)
{
int count[10];
float salaries[5];
long distances[10];

printf("Address of the array count is %x &count is %x\n",


count, &count);
printf("Address of the array salaries is %x &count is %x\n",
salaries, &salaries);
printf("Address of the array distances is %x &distances is %x\n",
distances, &distances);
}
#include <stdio.h>

void show_array(int values[], int number_of_elements)


{
int i;

for (i = 0; i < number_of_elements; i++)


printf("%d\n", values[i]);
}

void main(void)
{
int scores[5] = {70, 80, 90, 100, 90};

show_array(scores, 5);
}

#include <stdio.h>

void show_array(int values[], int number_of_elements)


{
int i;

printf("About to display %d values\n", number_of_elements);


for (i = 0; i < number_of_elements; i++)
printf("%d\n", values[i]);
}

void main(void)
{
int scores[5] = {70, 80, 90, 100, 90};
int count[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int small[2] = {-33, -44};

show_array(scores, 5);
show_array(count, 10);
show_array(small, 2);
}
#include <stdio.h>

int binary_search(int array[], int value, int size)


{
int found = 0;
int high = size, low = 0, mid;

mid = (high + low) / 2;

printf("\n\nLooking for %d\n", value);

while ((! found) && (high >= low))


{
printf("Low %d Mid %d High %d\n", low, mid, high);

if (value == array[mid])
found = 1;
else if (value < array[mid])
high = mid - 1;
else
low = mid + 1;

mid = (high + low) / 2;


}
return((found) ? mid: -1);
}

void main(void)
{
int array[100], i;

for (i = 0; i < 100; i++)


array[i] = i;

printf("Result of search %d\n", binary_search(array, 33, 100));


printf("Result of search %d\n", binary_search(array, 75, 100));
printf("Result of search %d\n", binary_search(array, 1, 100));
printf("Result of search %d\n", binary_search(array, 1001, 100));
}

#include <stdlib.h>
#include <stdio.h>

int compare_int(int *a, int *b)


{
return(*a - *b);
}

int compare_float(float *a, float *b)


{
return((*a == *b) ? 0: 1);
}

void main(void)
{
int int_values[] = {1, 3, 2, 4, 5};
float float_values[] = {1.1, 3.3, 2.2, 4.4, 5.5};

int *int_ptr, int_value = 2, elements = 5;


float *float_ptr, float_value = 33.3;

int_ptr = bsearch(&int_value, int_values,


elements, sizeof(int),
(int (*) (const void *, const void *)) compare_int);

if (*int_ptr)
printf("Value %d found\n", int_value);
else
printf("Value %d not found\n", int_value);

float_ptr = bsearch(&float_value, float_values,


elements, sizeof(float),
(int (*) (const void *, const void *)) compare_float);

if (*float_ptr)
printf("Value %3.1f found\n", float_value);
else
printf("Value %3.1f not found\n", float_value);

#include <stdio.h>
#include <stdlib.h>

void bubble_sort(int array[], int size)


{
int temp, i, j;

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


for (j = 0; j < size; j++)
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

void main(void)
{
int values[30], i;

for (i = 0; i < 30; i++)


values[i] = rand() % 100;

bubble_sort(values, 30);

for (i = 0; i < 30; i++)


printf("%d ", values[i]);
}

#include <stdio.h>
struct Shape {
int type;
int color;
float radius;
float area;
float perimeter;
};

void change_structure(struct Shape *shape)


{
(*shape).type = 0;
(*shape).color = 1;
(*shape).radius = 5.0;
(*shape).area = 22.0 / 7.0 * (*shape).radius * (*shape).radius;
(*shape).perimeter = 2.0 * 22.0 / 7.0 * (*shape).radius;
}

void main(void)
{
struct Shape circle;

change_structure(&circle);

printf("circle.type %d\n", circle.type);


printf("circle.color %d\n", circle.color);
printf("circle.radius %f circle.area %f circle.perimeter %f\n",
circle.radius, circle.area, circle.perimeter);
}

#include <stdio.h>

struct Shape {
int type;
int color;
float radius;
float area;
float perimeter;
};

void change_structure(struct Shape *shape)


{
shape->type = 0;
shape->color = 1;
shape->radius = 5.0;
shape->area = 22.0 / 7.0 * shape->radius * shape->radius;
shape->perimeter = 2.0 * 22.0 / 7.0 * shape->radius;
}

void main(void)
{
struct Shape circle;

change_structure(&circle);

printf("circle.type %d\n", circle.type);


printf("circle.color %d\n", circle.color);
printf("circle.radius %f circle.area %f circle.perimeter %f\n",
circle.radius, circle.area, circle.perimeter);
}

#include <stdio.h>
#include <dos.h>

void main (void)


{
struct date curr_date;

getdate(&curr_date);

printf("Current date: %d-%d-%d\n", curr_date.da_mon,


curr_date.da_day, curr_date.da_year);
}
#include <stdio.h>

void main(void)
{
int scores[5] = {80, 70, 90, 85, 80};

printf("Array Values\n");
printf("scores[0] %d\n", scores[0]);
printf("scores[1] %d\n", scores[1]);
printf("scores[2] %d\n", scores[2]);
printf("scores[3] %d\n", scores[3]);
printf("scores[4] %d\n", scores[4]);
}
#include <stdio.h>

void show_2d_array(int array[][10], int rows)


{
int i, j;

for (i = 0; i < rows; i++)


for (j = 0; j < 10; j++)
printf("array[%d][%d] = %d\n", i, j, array[i][j]);
}

void main(void)
{
int a[1][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
int b[2][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
int c[3][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30}};

show_2d_array(a, 1);
show_2d_array(b, 2);
show_2d_array(c, 3);
}
#include <stdio.h>
#include <dos.h>

void main(void)
{
union REGS inregs, outregs;
inregs.h.ah = 0x30;
inregs.h.al = 0;
intdos(&inregs, &outregs);

printf("Current version %d.%d\n", outregs.h.al, outregs.h.ah);


}

#include <stdio.h>
#include <dos.h>

void main(void)
{
union REGS inregs, outregs;

inregs.x.ax = 0x3000;
intdos(&inregs, &outregs);

printf("Current version %d.%d\n", outregs.x.ax & 0xFF,


outregs.x.ax >> 8);
}

#include <stdio.h>
#include <malloc.h>

void main (void)


{
int i;

float huge *values;

if ((values = (float huge *) halloc (17000,


sizeof(float))) == NULL)
printf ("Error allocating huge array\n");
else
{
printf("Filling the array\n");

for (i = 0; i < 17000; i++)


values[i] = i * 1.0;

for (i = 0; i < 17000; i++)


printf ("%8.1f ", values[i]);

hfree(values);
}
}
#include <stdio.h>

void main(void)
{
struct Shape {
int type;
int color;
float radius;
float area;
float perimeter;
} circle = {0, 1, 5.0, 78.37, 31.42};

printf("circle.type %d\n", circle.type);


printf("circle.color %d\n", circle.color);
printf("circle.radius %f circle.area %f circle.perimeter %f\n",
circle.radius, circle.area, circle.perimeter);
}

#include <stdlib.h>
#include <stdio.h>

int compare_int(int *a, int *b)


{
return(*a - *b);
}

int compare_float(float *a, float *b)


{
return((*a == *b) ? 0: 1);
}

void main(void)
{
int int_values[] = {1, 3, 2, 4, 5};
float float_values[] = {1.1, 3.3, 2.2, 4.4, 5.5};

int *int_ptr, int_value = 2, elements = 5;


float *float_ptr, float_value = 33.3;

int_ptr = lfind(&int_value, int_values,


&elements, sizeof(int),
(int (*) (const void *, const void *)) compare_int);

if (*int_ptr)
printf("Value %d found\n", int_value);
else
printf("Value %d not found\n", int_value);

float_ptr = lfind(&float_value, float_values,


&elements, sizeof(float),
(int (*) (const void *, const void *)) compare_float);

if (*float_ptr)
printf("Value %3.1f found\n", float_value);
else
printf("Value %3.1f not found\n", float_value);

#include <stdlib.h>
#include <stdio.h>

int compare_int(int *a, int *b)


{
return(*a - *b);
}

int compare_float(float *a, float *b)


{
return((*a == *b) ? 0: 1);
}

void main(void)
{
int int_values[10] = {1, 3, 2, 4, 5};
int *int_ptr, int_value = 1001, elements = 5, i;

printf("Array contents before search\n");


for (i = 0; i < elements; i++)
printf("%d ", int_values[i]);

int_ptr = lsearch(&int_value, int_values,


&elements, sizeof(int),
(int (*) (const void *, const void *)) compare_int);

printf("\nArray contents after search\n");


for (i = 0; i < elements; i++)
printf("%d ", int_values[i]);

#include <stdio.h>

void main(void)
{
int box[3][3];

float year_sales[52][5];

char pages[40][60][20];

printf("Bytes to hold int box[3][3] %d bytes\n", sizeof(box));


printf("Bytes to hold float year_sales[52][5] %d bytes\n",
sizeof(year_sales));
printf("Bytes to hold char pages[40][60][20] %ld bytes\n",
sizeof(pages));
}

#include <stdio.h>

void main(void)
{
int int_values[] = {51, 23, 2, 44, 45};
float float_values[] = {21.1, 13.3, 22.2, 34.4, 15.5};

printf("Number of elements in int_values %d\n",


sizeof(int_values) / sizeof(int_values[0]));

printf("Number of elements in float_values %d\n",


sizeof(float_values) / sizeof(float_values[0]));

#include <stdio.h>

int get_result(int a, int b, int (*compare)())


{
return(compare(a, b)); // Invoke the function passed
}

int max(int a, int b)


{
printf("In max\n");
return((a > b) ? a: b);
}

int min(int a, int b)


{
printf("In min\n");
return((a < b) ? a: b);
}

void main(void)
{
int result;

result = get_result(1, 2, &max);


printf("Max of 1 and 2 is %d\n", result);

result = get_result(1, 2, &min);


printf("Min of 1 and 2 is %d\n", result);
}

#include <stdio.h>

void main(void)
{
int counter = 10;
int *iptr; // Declare pointer value

iptr = &counter; // Assign the address


printf("Addres in iptr %x Value at *iptr %d\n", iptr, *iptr);

*iptr = 25; // Change the value in memory

printf("Value of counter %d\n", counter);


}

#include <stdio.h>
#include <ctype.h>

char *string_uppercase(char *string)


{
char *starting_address;

starting_address = string;

while (*string)
toupper(*string++);

return(starting_address);
}

void main(void)
{
char *title = "Jamsa's 1001 C/C++ Tips";
char *string;

string = string_uppercase(title);
printf("%s\n", string);

printf("%s\n", string_uppercase("Arrays and Pointers"));


}
#include <stdio.h>

void main(void)
{
int values[5] = {1, 2, 3, 4, 5};
int counter;
int *iptr;

iptr = values;

for (counter = 0; counter < 5; counter++)


{
printf("%d\n", *iptr);
iptr++;
}
}

#include <stdio.h>

int what_is_the_value(int ***ptr)


{
return(***ptr);
}

void main(void)
{
int *level_1, **level_2, ***level_3, value = 1001;

level_1 = &value;
level_2 = &level_1;
level_3 = &level_2;

printf("The value is %d\n", what_is_the_value(level_3));


}

#include <stdlib.h>
#include <stdio.h>

int compare_int(int *a, int *b)


{
return(*a - *b);
}

int compare_float(float *a, float *b)


{
if (*a < *b)
return(-1);
else if (*a == *b)
return(0);
else
return(1);
}

void main(void)
{
int int_values[] = {51, 23, 2, 44, 45};
float float_values[] = {21.1, 13.3, 22.2, 34.4, 15.5};

int elements = 5, i;

qsort(int_values, elements, sizeof(int),


(int (*) (const void *, const void *)) compare_int);

for (i = 0; i < elements; i++)


printf("%d ", int_values[i]);

putchar('\n');

qsort(float_values, elements, sizeof(float),


(int (*) (const void *, const void *)) compare_float);

for (i = 0; i < elements; i++)


printf("%4.1f ", float_values[i]);
}

#include <stdio.h>
#include <stdlib.h>

void quick_sort(int array[], int first, int last)


{
int temp, low, high, list_separator;

low = first;
high = last;
list_separator = array[(first + last) / 2];

do {
while (array[low] < list_separator)
low++;

while (array[high] > list_separator)


high--;

if (low <= high)


{
temp = array[low];
array[low++] = array[high];
array[high--] = temp;
}
} while (low <= high);

if (first < high)


quick_sort(array, first, high);
if (low < last)
quick_sort(array, low, last);
}
void main(void)
{
int values[100], i;

for (i = 0; i < 100; i++)


values[i] = rand() % 100;

quick_sort(values, 0, 99);

for (i = 0; i < 100; i++)


printf("%d ", values[i]);
}

#include <stdio.h>
#include <stdlib.h>

void selection_sort(int array[], int size)


{
int temp, current, j;

for (current = 0; current < size; current++)


for (j = current + 1; j < size; j++)
if (array[current] > array[j])
{
temp = array[current];
array[current] = array[j];
array[j] = temp;
}
}

void main(void)
{
int values[30], i;

for (i = 0; i < 30; i++)


values[i] = rand() % 100;

selection_sort(values, 30);

for (i = 0; i < 30; i++)


printf("%d ", values[i]);
}

#include <stdio.h>
#include <stdlib.h>

void shell_sort(int array[], int size)


{
int temp, gap, i, exchange_occurred;

gap = size / 2;

do {
do {
exchange_occurred = 0;

for (i = 0; i < size - gap; i++)


if (array[i] > array[i + gap])
{
temp = array[i];
array[i] = array[i + gap];
array[i + gap] = temp;
exchange_occurred = 1;
}
} while (exchange_occurred);
} while (gap = gap / 2);
}

void main(void)
{
int values[50], i;

for (i = 0; i < 50; i++)


values[i] = rand() % 100;

shell_sort(values, 50);

for (i = 0; i < 50; i++)


printf("%d ", values[i]);
}

#include <stdio.h>

void main(void)
{
int row, column;

float table[3][5] = {{1.0, 2.0, 3.0, 4.0, 5.0},


{6.0, 7.0, 8.0, 9.0, 10.0},
{11.0, 12.0, 13.0, 14.0, 15.0}};

for (row = 0; row < 3; row++)


for (column = 0; column < 5; column++)
printf("table[%d][%d] = %f\n", row, column, table[row][column]);
}

#include <stdio.h>

void main(void)
{
int row, column, table;

float values[2][3][5] = {
{{1.0, 2.0, 3.0, 4.0, 5.0},
{6.0, 7.0, 8.0, 9.0, 10.0},
{11.0, 12.0, 13.0, 14.0, 15.0}},

{{16.0, 17.0, 18.0, 19.0, 20.0},


{21.0, 22.0, 23.0, 24.0, 25.0},
{26.0, 27.0, 28.0, 29.0, 30.0}}
};

for (row = 0; row < 2; row++)


for (column = 0; column < 3; column++)
for (table = 0; table < 5; table++)
printf("values[%d][%d][%d] = %f\n", row, column, table,
values[row][column][table]);
}
#include <stdio.h>

void main(void)
{
int scores[5] = {80, 70, 90, 85, 80};
int i;

printf("Array Values\n");

for (i = 0; i < 5; i++)


printf("scores[%d] %d\n", i, scores[i]);
}
#include <stdio.h>

void show_string(char *string)


{
while (*string)
putchar(*string++);
}

void main(void)
{
show_string("Jamsa's 1001 C/C++ Tips");
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void bubble_sort(char *array[], int size)


{
char *temp;
int i, j;

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


for (j = 0; j < size; j++)
if (strcmp(array[i], array[j]) < 0)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

void main(void)
{
char *values[] = {"AAA", "CCC", "BBB", "EEE", "DDD"};
int i;

bubble_sort(values, 5);

for (i = 0; i < 5; i++)


printf("%s ", values[i]);
}

#include <stdio.h>

void main(void)
{
struct Date {
char month_name[64];
int month;
int day;
int year;
} current_date = { "July", 7, 4, 1994 };

int i;

for (i = 0; current_date.month_name[i]; i++)


putchar(current_date.month_name[i]);
}

#include <stdio.h>

struct Shape {
int type;
int color;
float radius;
float area;
float perimeter;
};

void show_structure(struct Shape shape)


{
printf("shape.type %d\n", shape.type);
printf("shape.color %d\n", shape.color);
printf("shape.radius %f shape.area %f shape.perimeter %f\n",
shape.radius, shape.area, shape.perimeter);
}

void main(void)
{
struct Shape circle;

circle.type = 0;
circle.color = 1;
circle.radius = 5.0;
circle.area = 22.0 / 7.0 * circle.radius * circle.radius;
circle.perimeter = 2.0 * 22.0 / 7.0 * circle.radius;

show_structure(circle);
}

#include <stdio.h>

long sum_array(int array[], int elements)


{
long sum = 0;

int i;

for (i = 0; i < elements; i++)


sum += array[i];

return(sum);
}
void main(void)
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[2][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
int c[3][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30}};

printf("Sum of first array elements %d\n", sum_array(a, 10));


printf("Sum of second array elements %d\n", sum_array(b, 20));
printf("Sum of third array elements %d\n", sum_array(c, 30));
}
#include <stdio.h>

void swap_values(int *a, int *b)


{
int temp;

temp = *a; // Temporarily hold the value pointed to by a


*a = *b; // Assign b's value to a
*b = temp; // Assign a's value to b
}

void main(void)
{
int one = 1, two = 2;

swap_values(&one, &two);

printf("one contains %d two contains %d\n", one, two);


}

void main(void)
{
char string[66000L]; // 66,000 bytes

int values[33000L]; // 33,000 * 2 = 66,000 bytes

float numbers[17000]; // 17,000 * 4 = 68,000 bytes


}

#include <stdio.h>

void main(void)
{
union EmployeeDates {
int days_worked;
struct Date {
int month;
int day;
int year;
} last_day;
} emp_info;

union Numbers {
int a;
float b;
long c;
double d; // Largest--requires 8 bytes
} value;

printf("Size of EmployeeDates %d bytes\n", sizeof(emp_info));


printf("Size of Numbers %d bytes\n", sizeof(value));
}

#include <stdio.h>

void main(void)
{
char *weekdays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};

int i;

for (i = 0; i < 7; i++)


printf("weekdays[%d] contains %s\n", i, weekdays[i]);
}

#include <stdio.h>

void main(void)
{
char *workdays[] = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "" };
char **work_day;

work_day = workdays;

while (*work_day)
printf("%s\n", *work_day++);
}

You might also like