Classic C Program (13/20)

Publisher:chinapxfLatest update time:2015-03-09 Source: laogu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Title: Output an array in reverse order.
1. Program analysis: Swap the first with the last.
2. Program source code:
#define N 5
main()
{ int a[N]={9,6,5,4,1},i,temp;
 printf(" original array: ");
 for(i=0;i  printf("%4d",a[i]);
 for(i=0;i  {temp=a[i];
  a[i]=a[Ni-1];
  a[Ni-1]=temp;
 }
printf(" sorted array: ");
for(i=0;i  printf("%4d",a[i]);
}
 

 

Title: There is an array that has been sorted. Now enter a number and insert it into the array according to the original rule.
1. Program analysis: First determine whether this number is greater than the last number, then consider the situation of inserting the middle number. After the insertion,
     the numbers after this element will be moved back one position in turn. 
2. Program source code:
main()
{
int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;  printf(
"original array is: "); for (
i=0;i<  10;i++) printf   ( "%5d",a[i]); printf(" "  )     ;    printf    (    "     insert   a new    number     ;    } }    } for(i=0;i<11;i++)  printf("%6d",a[i]); }























 

 

Title: Find the sum of the diagonal elements of a 3*3 matrix 
1. Program analysis: Use a double for loop to control the input of a two-dimensional array, and then add a[i][i] and output it.
2. Program source code:
main()
{
float a[3][3],sum=0;
int i,j;
printf("please input rectangle element: ");
for(i=0;i<3;i++)
 for(j=0;j<3;j++)
 scanf("%f",&a[i][j]);
for(i=0;i<3;i++)
 sum=sum+a[i][i];
printf("duijiaoxian he is %6.2f",sum);
}

 

Title: Sort 10 numbers
1. Program analysis: You can use the selection method, that is, from the last 9 comparisons, select a smallest one and exchange it with the first element, and
      then repeat the same process, that is, compare the second element with the last 8 and exchange them.        
2. Program source code:
#define N 10
main()
{int i,j,min,tem,a[N];
/*input data*/
printf("please input ten num: ");
for(i=0;i {
printf("a[%d]=",i);
scanf("%d",&a[i]);}
printf(" ");
for(i=0;i printf("% 5d",a[i]);
printf(" ");
/*sort ten num*/
for(i=0;i {min=i;
for(j=i+1;j if(a[min]>a[j]) min=j;
tem=a[i];
a[i]=a[min];
a[min]=tem;
}
/*output data*/
printf("After sorted ");
i=0;i printf("%5d",a[i]);
}

 

题目:求100之内的素数   
1.程序分析:
2.程序源代码:
#i nclude
#i nclude "math.h"
#define N 101
main()
{
int i,j,line,a[N];
for(i=2;i for(i=2;i  for(j=i+1;j  {
  if(a[i]!=0&&a[j]!=0)
  if(a[j]%a[i]==0)
  a[j]=0;}
printf(" ");
for(i=2,line=0;i {
 if(a[i]!=0)
 {printf("%5d",a[i]);
 line++;}
 if(line==10)
 {printf(" ");
line=0;}
}
}

 

Reference address:Classic C Program (13/20)

Previous article:Design of relay drive system based on dual hot standby structure of AT90CAN128
Next article:Classic C Program (12/20)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号