c_cpp c中的数组和循环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp c中的数组和循环相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
// print all the elements of array
int main(){
int niz[5]={10, 20,5,9,7};
for(int i=0; i<5; i++){
printf("%d\n", niz[i]);
}
}
// user input elements in array and print that numbers
int main(){
int niz[100], n, i, j;
printf("Unesite broj elemenata niza: ");
scanf("%d", &n);
for(i=0; i<n; i++){
printf("\nEnter %d element: ", i+1);
scanf("%d", &niz[i]);
}
printf("\n Elements are: ");
for(j=0; j<n; j++){
printf("%5d", niz[j]);
}
}
// print the elements of array in reverse order
int main(){
int niz[100]={10,20,50,40, 60};
printf("Elemens in reverse order is given below: \n");
for(int i=4; i>=0; i--){
printf("%5d", niz[i]);
}
}
// duzina niza intigeer
int main(){
int niz[]={1,2,3,4};
printf("%d", sizeof(niz)/sizeof(int));
return 0;
}
// duzina niza string
void main(){
int duzina;
char niz1[100] = "Ja sam student ITS-a, Nenad Stanojev";
duzina = strlen(niz1);
printf("%d", duzina);
}
// Find sum of all the lements of array
void main(){
int a[100];
int i,n, sum=0;
printf("Input the number of elements to be stored in the array:");
scanf("%d", &n);
printf("Input %d elements in array: \n", n);
for(i=0; i<n; i++){
printf("Element - %d: ->>", i);
scanf("%d", &a[i]);
}
for(i=0; i<n; i++){
sum+=a[i];
}
printf("\nSum of all elements stored in the array is: %d", sum);
}
// sabiranje elemenata iz dva niza
int main(){
int a[40], b[40], c[40], n;
printf("How meny number you want to store in a single array\n");
scanf("%d", &n);
printf("Enter %d elements of first array one by one\n", n);
for(int i=0;i<n;i++){
scanf("%d", &a[i]);
}
printf("Enter %d elements of second array one by one\n", n);
for(int j=0; j<n; j++){
scanf("%d", &b[j]);
}
printf("Suma elemenata po indeksu: \n");
for(int k=0; k<n; k++){
c[k]=a[k]+b[k];
printf("%5d", c[k]);
}
}
// How meny number you want to store in a single array
// 4
// Enter 4 elements of first array one by one
// 4
// 5
// 6
// 8
// Enter 4 elements of first array one by one
// 8
// 7
// 9
// 5
// Suma elemenata po indeksu: 12 12 15 13
// Print all even number of array
int main(){
int niz[100], n;
printf("Enter how many number you wanna store :\n");
scanf("%d", &n);
printf("Enter %d number one by one\n", n);
for(int i=0; i<n; i++){
scanf("%d", &niz[i]);
}
printf("Array element is given below\n");
for(int j=0; j<n; j++){
printf("%5d", niz[j]);
}
printf("\n Even numbers in array are given bellow: \n ");
for(int k=0; k<n; k++){
if(niz[k]%2==0){
printf("%5d", niz[k]);
}
}
}
// Print all odd number of array
int main(){
int niz[100], n;
printf("Enter how many number you wanna store :\n");
scanf("%d", &n);
printf("Enter %d number one by one\n", n);
for(int i=0; i<n; i++){
scanf("%d", &niz[i]);
}
printf("Array element is given below\n");
for(int j=0; j<n; j++){
printf("%5d", niz[j]);
}
printf("\n Even numbers in array are given bellow: \n ");
for(int k=0; k<n; k++){
if(niz[k]%2!=0){
printf("%5d", niz[k]);
}
}
}
//Fiind maximum and minimum value form array
void main(){
int arr[100];
int i, maximum, minimum, n;
printf("Unesite koliko zelite elemenata u nizu: ");
scanf("%d", &n);
printf("Please enter %d elements in array:\n", n);
for(i=0; i<n; i++){
printf("Element %d:--> ", i);
scanf("%d", &arr[i]);
}
maximum=arr[0];
minimum=arr[0];
for(i=1; i<n; i++){
if(arr[i]> maximum){
maximum=arr[i];
}
if(arr[i]<minimum){
minimum=arr[i];
}
}
printf("Maximum element of array you entered is : %d\n", maximum);
printf("Minimum element of array you entered is : %d\n", minimum);
}
// Search element in array
int main(){
int niz[11]={1,2,31,3,6,6,8,99,49,78,89};
int n, label=0;
printf("Elementi niza:\n");
for(int i=0; i<10; i++){
printf("%4d", niz[i]);
}
printf("\n Koji broj trazite? : ");
scanf("%d", &n);
for(int j=1; j<10; j++){
if(n==niz[j]){
printf("Trazeni element je na poziciji: %d", j+1);
label=1;
}
}
if(label==0){
printf("Element nije progradjen");
}
}
// merge two arrays
int main() {
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;
printf("\nEnter no of elements in 1st array :");
scanf("%d", &n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("\nEnter no of elements in 2nd array :");
scanf("%d", &n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
i = 0;
j = 0;
k = 0;
// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
} else {
res[k] = arr2[j];
k++;
j++;
}
}
// Some elements in array 'arr1' are still remaining
// where as the array 'arr2' is exhausted
while (i < n1) {
res[k] = arr1[i];
i++;
k++;
}
// Some elements in array 'arr2' are still remaining
// where as the array 'arr1' is exhausted
while (j < n2) {
res[k] = arr2[j];
k++;
j++;
}
//Displaying elements of array 'res'
printf("\nMerged array is :");
for (i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);
return (0);
}
// Enter no of elements in 1st array : 4
// 11 22 33 44
//
// Enter no of elements in 2nd array : 3
// 10 40 80
//
// Merged array is : 10 11 22 33 40 44 80
// Racunanje ukupan broj duplikata ako ih ima
int main(){
int arr[10], i, j, Size, Count=0;
printf("Please enter number of elements in an array: " );
scanf("%d", &Size);
printf("\nPlease enter %d element of an array: \n", Size);
for(i=0; i< Size; i++){
scanf("%d", &arr[i]);
}
for(i=0; i<Size; i++){
for(j=i+1; j<Size; j++){
if(arr[i]== arr[j]){
Count++;
break;
}
}
}
printf("\nTotal number of duplicate elements in this array = %d", Count);
return 0;
}
// 2d arrays
int main(){
// 2D array declaration
int disp[2][3];
// Counter variables for the loop
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
// unos matrice i racunanje sumu glavne i sporedne dijagonale
void main ()
{
static int array[10][10];
int i, j, m, n, a = 0, sum = 0;
printf("Enetr the order of the matix \n");
scanf("%d %d", &m, &n);
if (m == n )
{
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
for (i = 0; i < m; ++i)
{
sum = sum + array[i][i];
a = a + array[i][m - i - 1];
}
printf("\nThe sum of the main diagonal elements is = %d\n", sum);
printf("The sum of the off diagonal elements is = %d\n", a);
}
else
printf("The given order is not square matrix\n");
}
// Program za iracunavanje broja pojavljivanja datog znakova
// transponovana Matrica
void main(){
static int array[10][10];
int i,j,m,n;
printf("Enter the order of the matrix: \n");
scanf("%d %d", &m, &n);
printf("Enter the coeficients of the matrix\n");
for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%2d", array[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is \n");
for(j=0; j<n; j++){
for(i=0; i<m; i++){
printf("%2d", array[i][j]);
}
printf("\n");
}
}
// sort array in desc order
void main(){
int number[40];
int i,j,a,n;
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for(i=0; i<n; i++){
scanf("%d", &number[i]);
}
for(i=0; i<n; i++){
for(j=i+1; j<n; j++){
if(number[i]<number[j]){ // za asc order samo promeni znak (>)
a=number[i];
number[i]=number[j];
number[j]=a;
}
}
}
printf("The numbers arranged in descending order are given below: \n");
for(i=0; i<n; i++){
printf("%d\n", number[i]);
}
}
#include <stdio.h>
#include <string.h>
// LINEAR SEARCH
int main(){
int niz[10]={14,52,62,37, 43,42,93,92, 97, 98};
int r, m=0;
printf("Niz svih brojeva \n");
int duzina_niza=sizeof(niz)/sizeof(int);
for(int i=0; i<=duzina_niza; i++){
printf("%5d", niz[i]);
}
printf("\n Unesite broj koji pretrazujete: ");
scanf("%d", &r);
for(int j=1; j<=duzina_niza; j++){
if(r==niz[j]){
printf("Zeljeni element je pronadjen na poziciji: %d", j+1);
m=1;
}
}
if(m==0){
printf("\nBroj nije pronadjen.");
}
}
// BUBBLE SORT
int main()
{
int data[100],i,n,step,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1]) //To sort in descending order, change > to < in this line.
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
#include<stdio.h>
// suma brojeva na glavnoj dijagonali
void main(){
int i, j, niz[50][50], sum=0, n;
printf("Input the size of the square matrix: ");
scanf("%d", &n);
printf("Enter elements in the matrix: ");
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("element - [%d][%d] : ", i, j);
scanf("%d", &niz[i][j]);
if(i==j){
sum=sum+niz[i][j];
}
}
}
// show matrix
printf("The matrix is: \n" );
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%4d", niz[i][j]);
}
printf("\n");
}
printf("Sum of main diagonal is: %d ", sum);
}
// suma brojeva na sporednoj dijagonali
void main()
{
int i,j,arr1[50][50],sum=0,n,m=0;
printf("\n\nFind sum of left diagonals of a matrix :\n");
printf("---------------------------------------\n");
printf("Input the size of the square matrix : ");
scanf("%d", &n);
m=n;
printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("The matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++)
printf("% 4d",arr1[i][j]);
printf("\n");
}
// calculate the sum of left diagonals
for(i=0;i<n;i++)
{
m=m-1;
for(j=0;j<n ;j++)
{
if (j==m)
{
sum= sum+arr1[i][j];
}
}
}
printf("Addition of the left Diagonal elements is :%d\n",sum);
}
// naci drugi najveci element u nizu
void main(){
int niz[50], n, i, j=0, lrg, lrg2nd;
printf("Input the size of array: ");
scanf("%d", &n);
printf("Enter %d elements in the array: \n");
for(i=0; i<n; i++){
printf("element - %d : ", i);
scanf("%d", &niz[i]);
}
lrg=0;
for(i=0; i<n ;i++){
if(lrg<niz[i]){
lrg=niz[i];
j=i;
}
}
// ignore the largest element and find 2nd largest element in array;
lrg2nd=0;
for(i=0; i<n; i++){
if(i==j){
i++;
i--;
}else{
if(lrg2nd<niz[i]){
lrg2nd=niz[i];
}
}
}
printf("The secon largest element in array is: %d ", lrg2nd);
}
以上是关于c_cpp c中的数组和循环的主要内容,如果未能解决你的问题,请参考以下文章