C中元素的升序 [英] Ascending order of elements in C

查看:97
本文介绍了C中元素的升序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个包含9个元素的向量



vec [20] = 80.59 ,132.40,81.85,79.83,119.96,111.89,110.99,101.21,118.53



我试图将它们放入一个降序中



这就是控制台给我看的东西:

80.59 79.83 81.85 101.21 110.99 111.89 118.53 119.96 132.40





Up是我使用过的表格但不起作用。

我该怎么办?我在C编程工作。



我尝试过:



Hi guys

I have a vector with 9 elements

vec[20]= 80.59 , 132.40, 81.85, 79.83, 119.96, 111.89, 110.99, 101.21, 118.53

Im trying to put them in an anscending order

thats what the console shows me:
80.59 79.83 81.85 101.21 110.99 111.89 118.53 119.96 132.40


Up is the formule i used but it is not working.
What Should I do? I am working in C Programming.

What I have tried:

for(k=1;k<=i-1;k++)
{
for(j=k+1;j++) {
if(vec[k]>vec[j])
{
    aux=vec[k];
    vec[k]=vec[j];
    vec[j]=aux;
}
}
}

推荐答案

C 编程语言标准库轻轻提供 qsort [ ^ ]功能:

The C programming language standard library gently provides the qsort[^] function:
#include <stdio.h>
#include <stdlib.h>

int comp ( const void * pa, const void *pb)
{
  return ( *(double *)(pa) > *(double*)(pb));
}


int main()
{
  size_t n;

  double a[]  = { 80.59 , 132.40, 81.85, 79.83, 119.96, 111.89, 110.99, 101.21, 118.53};

  const size_t LENGTH = sizeof(a)/sizeof(a[0]);

  qsort( a, LENGTH, sizeof(a[0]),  comp);

  for (n = 0; n<LENGTH; ++n)
    printf("%g\n", a[n]);

  return 0;
}


不惊讶 - 不编译:

Not surprised - that doesn't compile:
for(j=k+1;j++) {

循环有三个部分:

A for loop has three sections:

for (init; limit; increment)
   {
   ...
   }

自己做两个好处:

1)缩进你的代码!

2)选择一个包围系统并坚持下去!



混合和匹配只会让人感到困惑并导致错误。

And do yourself two favours:
1) Indent your code!
2) Pick a bracketing system and stick to it!

Mixing and matching just gets confusing and leads to mistakes.

for(k = 1; k < i; k++)
    {
    for(j = k + 1; j < i - 1; j++)
        {
        if(vec[k]>vec[j])
            {
            aux = vec[k];
            vec[k] = vec[j];
            vec[j] = aux;
            }
        }
    }


C语言基于0,这意味着在数组中,第一个元素位于第0位。

C language is 0 based, this mean that in an array, the first element is at position 0.
vec[20]= 80.59 , 132.40, 81.85, 79.83, 119.96, 111.89, 110.99, 101.21, 118.53



假设这就是你想要使用,而不是正确的C代码。

数组 vec 大小为20,元素位于0到19之间,只有9个第一个元素被初始化。

你的代码不完整,我们只能猜测没有其他错误。



学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。


Assuming this is what you want to use, and not a correct piece of c code.
The array vec is of size 20 and elements are at position from 0 to 19, and only the 9 first elements are initialized.
Your code being incomplete, we can only guess that there is not other error.

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

for(k=1;k<=i-1;k++)
{
  for(j=k+1;j++) {
    if(vec[k]>vec[j])
    {
      aux=vec[k];
      vec[k]=vec[j];
      vec[j]=aux;
    }
  }
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]


这篇关于C中元素的升序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆