Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of programming revision in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "C language programming modification problem example analysis". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and study the "C language programming modification problem case analysis" together.

1. Topic description

In a given program modi1.c, the function of fun is to convert a decimal positive integer m into a k (2 ≤ k ≤ 9) binary number and output it bit by bit.

For example, if you enter 8 and 2, you should output 1000 (that is, the decimal number 8 converted to binary is 1000).

Please correct the error in the program so that it can get the correct result.

Note: do not change the main function, do not add or delete lines, and do not change the structure of the program!

2. Original program

# include

# include

Void fun (int m, int k)

{

Int aa [20], i

For (I = 0; m; iTunes +)

{

/ * found*/

Aa [I] = mUnik

M / = k

}

For (; I; iMurray -)

/ * found*/

Printf ("% d", aa [I])

}

Main ()

{

Int b, n

Printf ("\ nPlease enter a number and a base:\ n")

Scanf ("d d", & n, & b)

Fun (n, b)

Printf ("\ n")

Getchar ()

}

3. Reference answer

The examination point of this question is the transformation of the number system.

K-ary numbers have the following characteristics

1. There are a total of k digits from 0 to kMui 1.

two。 Every time k enters 1, borrow 1 when k.

The number on a bit of a k-ary number actually represents the weight of that number multiplied by that bit, such as the decimal number 12, which actually represents 1: 10. In the fun function given by the original program, the first for loop is used to convert the decimal integer data m into k data, and the results are stored in the array aa in the order from low to high. After the for loop, I is the number of digits of the converted k digits, so according to the title requirements, the first place should be changed to "aa [I] = m% k;" or the same sentence.

The second error is that the values in the array containing I are printed from back to front, and the subscript should be [imur1] to [0]. So "printf ("% d ", aa [I]);" should be replaced by "printf ("% d ", AA [I-1]);".

This analysis is for reference only.

Correct code:

# include

# include

Void fun (int m, int k)

{

Int aa [20], i

For (I = 0; m; iTunes +)

{

/ * found*/

Aa [I] = m% k

M / = k

}

For (; I; iMurray -)

/ * found*/

Printf ("% d", aa [iMur1])

}

Main ()

{

Int b, n

Printf ("\ nPlease enter a number and a base:\ n")

Scanf ("d d", & n, & b)

Fun (n, b)

Printf ("\ n")

Getchar ()

}

Thank you for your reading. the above is the content of "C language programming modification problem example analysis". After the study of this article, I believe you have a deeper understanding of the problem of c language programming modification problem example analysis. the specific use of the problem also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report