c4-One-dimensional arrays

Source: Internet
Author: User
Tags greatest common divisor

//

Main.m

C4_ One-dimensional arrays

//

Created by Dllo on 15/7/3.

Copyright (c) 2015 Zhozhicheng. All rights reserved.

//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

Least common multiple

int m = 0,n = 0;

scanf ("%d%d", &m,&n);

int num = m > n? m:n;

Used to save the found least common multiple.

int minnum = 0;

for (int i = num; I >= 1; i++) {

if (i% m = 0 && i% n ==0) {

Minnum = i;

Break

//        }

//

//    }

printf ("%d\n", minnum);

Greatest common divisor

int m = 0,n = 0;

scanf ("%d%d", &m,&n);

//

int min = m < n? m:n;

Used to save greatest common divisor

int num = 0;

for (int i = min; I >= 1; i--) {

If (m% i = = 0 && N i = = 0) {

num = i;

Break

//        }

}printf ("%d\n", num);

//

//

Euclidean method

int m = 0,n = 0;

scanf ("%d%d", &m,&n);

//

while (m% n! = 0) {

int temp = m% n;

Give the N to M

m = n;

Results to N

n = temp;

//

}printf ("%d\n", N);

//

Define a one-dimensional array

int arr[5] = {1,2,3,4,5};

printf ("%d\n", arr[4]);

C language, the system to face the array of monthly knot will produce a warning, the current element to be accessed exceeds the maximum value of subscript, OC array out of bounds will cause a crash phenomenon

Array overflow

Iterating through an array

int arr[5]= {2,1};

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

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

//    }

//

int arr[20]={0};

int sum = 0;

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

Arr[i] = arc4random ()% (70-30 + 1) + 30;

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

sum = arr[i] + sum;

//    }

printf ("%d\n", sum);

Exercise 1

int arr[20] ={0};

Range 30-70

//

Define a variable to hold and value

int sum = 0;

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

Arr[i] = arc4random ()% (70-30 + 1) + 30;

sum = sum + arr[i];

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

//    }

//

printf ("%d\n", sum);

int arr[20]={0};

int max = 0,min = 70;

Subscript to hold the array with the minimum maximum value

int maxindex = 0,minindex = 0;

//

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

Arr[i] = arc4random ()% (70-30 + 1) + 30;

if (Max < arr[i]) {

max = Arr[i];

Maxindex = i;

//

//        }

if (min > Arr[i]) {

min = Arr[i];

Minindex = i;

//        }

//

}printf ("%d\n%d\n", max,min);

printf ("%d\n%d\n", Maxindex,minindex);

//

Exercise 2

Copy two arrays

1. Definition of two arrays, same length

int arr[20] = {0},b[20] = {20};

Assign a random number to one of the arrays, range 30-70

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

Arr[i] = arc4random ()% (70-30 + 1) + 30;

Assigning operations to B arrays

B[i] = Arr[i];

//

//    }

Use arrays to save and value

Print out 1000 different situations

int arr[28] = {0};

//

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

for (int j = 0; J <; J + +) {

for (int k = 0; k < k++) {

Arr[i + j + k]++;

//

//            }

//        }

//    }

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

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

//    }

//

Exercise 3

int arr[10]={0},b[10]={0},c[10]={0};

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

Arr[i] = arc4random ()% (40-20 + 1) + 20;

B[i] = arc4random ()% (40-20 + 1) + 20;

C[i] = Arr[i] + b[i];

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

//    }

Bubble sort

int arr[6]={2,1,5,6,3,4};

Outer Loop-1: loop execution until there are two numbers not sorted, just need to exchange the unsorted two number, the number of executions is the number of arrays-1

for (int i = 0; i < 6-1; i++) {

Internal Loop-1: In order to prevent the array from overstepping, at the internal Loop-1

for (int j = 0; J < 6-1-I; j + +) {

if (Arr[j] > arr[j + 1]) {

int temp = 0;

temp = Arr[j];

arr[j]= arr[j + 1];

Arr[j + 1]= temp;

//            }

//    }

//    }

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

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

//

//    }

//

int arr[6]={2,1,5,6,3,4};

Re-write the colon sort again

for (int i = 0; i < 6-1; i++) {

for (int j = 0; J < 6-1-I; j + +) {

if (Arr[j] > arr[j + 1]) {

int temp = 0;

temp = Arr[j];

ARR[J] = arr [j + 1];

Arr[j + 1]= temp;

//            }

//        }

//    }

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

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

//

//    }

Randomly generates 10 [20,40] random numbers and sorts 10 numbers from small to large

Defines an array of length 10

int arr[10] = {0};

Generating random numbers, assigning operations to arrays

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

Arr[i] = arc4random ()% (40-20 + 1) + 20;

//    }

Sort operations on an array that has been copied to completion

operation on an array of assignment completion, no problem

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

for (int j = 0; J < 10-1-I; j + +) {

if (Arr[j] > arr[j+1]) {

int temp = 0;

temp = Arr[j];

Arr[j]= arr[j+1];

arr[j+1]= temp;

//            }

//        }

Print sort Results

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

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

//    }

//

Character arrays and strings

int arr[6]={1,2,3,4,5,6};

Character array

Char str[6]={' i ', ' P ', ' h ', ' o ', ' n ', ' e '};

The subscript is an assignment of 5 elements

STR[5] = ' W ';

//

//

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

printf ("%c\n", Str[i]);

//    }

String

Char str[]= "iphone";

Character array

Char str1[]={' i ', ' P ', ' h ', ' o ', ' n ', ' e '};

//

int arr[5]= {1,3,2,6,4};

printf ("%ld\n", sizeof (arr));

The difference is that the string is more than the character array at the end of where there is a ' s ', ' \ ' is the end of the string, the character array can be called string

Print a string

printf ("%s\n", str);

//

%s printing is done by looking for the string end flag ' \ s ' and hitting '/' will end printing

Some function functions on strings

Char str[] = "iphone";

//

String length

printf ("%l d\n", strlen (str));

Strlen is the number of characters that can be seen by the naked eye

printf ("%l d\n", sizeof (str));

sizeof is the size of the space occupied by the string including ' + '

Char str[] = "iphone";

Copy of the string

Char newstr[10]= "";

strcpy (Newstr, str);

printf ("%s\n", newstr);

Concatenation of strings

//

Char str1[20] = "I have";

Char str2[20] = "IPhone";

strcat (str1, str2);

printf ("%s\n", str1);

The string length to be spliced is long enough

Comparison of strings

Char str1[] = "ABA";

Char str2[] = "ADC";

printf ("%d\n", strcmp (STR1, str2));

Used to record the number of spaces

int number = 0;

Char str[] = "lads asdj hdh asdv Asdls";

for (int i = 0;i < strlen (str); i++) {

if (str[i]== ') {

Number = number + 1;

//                    }

}printf ("%d\n", number);

Char str[]= "KHFYOMJ";

Reverse output

for (int i = (int) strlen (str)-1; I >= 0; i--) {

printf ("%c", Str[i]);

//

//    }

printf ("\ n");

Reverse

Char str[] = {"World"};

int len = strlen (str);

for (int i =0; i < LEN/2; i++) {

char C = str[i];

Str[i] = str[len-1-i];

Str[len-1-I.] = C;

//

//    }

printf ("%s\n", str);

return 0;

}

c4-One-dimensional arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.