Results 1 to 6 of 6

Thread: Beginner in C + +, small question about a program

  1. #1
    Harry Crosby is offline Member
    Join Date
    Sep 2009
    Posts
    61
    Rep Power
    3

    Default Beginner in C + +, small question about a program

    Hello,
    Currently a student, I started in C + +.
    I exercise to create a program to sort 10-digit code here:

    Code:
    1.	# define N 10
    2.	
    3.	# include <iostream>
    4.	# include <stdlib.h>
    5.	using namespace std;
    6.	
    7.	pos_max int (int t [], int p);
    8.	int main ()
    9.	(
    10.	srand (NULL);
    11.	int t [N];
    12.	int m;
    13.	for (int i = 0; i <N; i + +)
    14.	(
    15.	
    16.	t [i] = rand ()% 9;
    17.	court <<t [i] << "";
    18.	)
    19.	court <<endl;
    20.	m = 0
    21.	for (; m <N, m + +)
    22.	(
    23.	court <<m << "" <<N <<endl;
    24.	int p = pos_max (t, Nm);
    25.	court <<p <<endl;
    26.	int to = t [p];
    27.	t [p] = t [Nm];
    28.	t [Nm] = aux;
    29.	)
    30.	for (int i = 0; i <N; i + +)
    31.	(
    32.	court <<t [i] << "";
    33.	)
    34.	return 0;
    35.	)
    36.	pos_max int (int * t, int p)
    37.	(
    38.	int pmax = 0;
    39.	for (int i = 0; i <p; i + +)
    40.	(
    41.	if (t [i]> t [pmax])
    42.	(
    43.	pmax = i;
    44.	)
    45.	)
    46.	return pmax;47.	)
    The problem is that the "m" starts at 10 (see The cost that I placed. Here are the result of a simulation:

    6 6 5 7 7 6 1 1 2 4
    10 10
    0
    6 6 5 7 7 6 1 1 2 4.

    I do not understand why m not start a 0 and 1 ....

    Thank you in advance

  2. #2
    Join Date
    Sep 2009
    Posts
    54
    Rep Power
    3

    Default

    Already have your code not compile, missing one, after m = 0

    After that t [Nm] that goes beyond the table should be replaced by t [Nm-1] as indexes in your table go from 0 to 9 but otherwise, once it compiles, m good start at 0

  3. #3
    Harry Crosby is offline Member
    Join Date
    Sep 2009
    Posts
    61
    Rep Power
    3

    Default

    I made the change that you told me, but I still have the same problem...

    Code:
    1.	# define N 10
    2.	# include <iostream>
    3.	# include <stdlib.h>
    4.	using namespace std;
    5.	
    6.	pos_max int (int t [], int p);
    7.	int main ()
    8.	(
    9.	srand (NULL);
    10.	int t [N];
    11.	int m;
    12.	/ / Making the array.
    13.	for (int i = 0; i <N; i + +)
    14.	(
    15.	
    16.	t [i] = rand ()% 9;
    17.	court <<t [i] << "";
    18.	)
    19.	court <<endl;
    20.	
    21.	
    22.	
    23.	for (m = 0, m <N, m + +)
    24.	(
    25.	court <<m << "" <<N <<endl;
    26.	int p = pos_max (t, Nm-1);
    27.	court <<p <<endl;
    28.	int to = t [p];
    29.	t [p] = t [Nm-1];
    30.	t [Nm-1] = aux;
    31.	)
    32.	for (int i = 0; i <N; i + +)
    33.	(
    34.	court <<t [i] << "";
    35.	)
    36.	return 0;
    37.	)
    38.	pos_max int (int t, int p)
    39.	(
    40.	int pmax = 0;
    41.	for (int i = 0; i <p; i + +)
    42.	(
    43.	if (t [i]> t [pmax])
    44.	(
    45.	pmax = i;
    46.	)
    47.	)
    48.	return pmax;49.	)

  4. #4
    Harvey Lavan is offline Member
    Join Date
    Sep 2009
    Posts
    57
    Rep Power
    3

    Default

    I already know how you do it to compile because your function pos_max "takes an int argument instead of int * (for t). After correcting it works.

    EDIT: I just saw that you had said the prototype correctly, cons by signing the definition is incorrect

  5. #5
    Join Date
    Sep 2009
    Posts
    54
    Rep Power
    3

    Default

    Nan but your call pos_max was well Nm is needed is that after things did not.

  6. #6
    Harry Crosby is offline Member
    Join Date
    Sep 2009
    Posts
    61
    Rep Power
    3

    Default

    Great, it works, I understood! thank you very much.

Similar Threads

  1. Question about new CPU
    By Benedict Patrician in forum CPU & Components
    Replies: 5
    Last Post: 10-16-2010, 12:28 PM
  2. Difficult Problem for Beginner
    By warner20 in forum Everything Else
    Replies: 1
    Last Post: 05-04-2009, 10:22 AM
  3. Difficult Problem for Beginner
    By watson29 in forum Everything Else
    Replies: 1
    Last Post: 05-02-2009, 09:44 AM
  4. Mr Site Takeaway website software beginner
    By Fabrico55 in forum General Software Terms
    Replies: 0
    Last Post: 12-15-2008, 01:16 PM
  5. My Question about
    By DYNCENRON in forum Graphic & Displays
    Replies: 0
    Last Post: 10-05-2008, 08:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48