Please help me on 2 small problems.

Hey i have 2 problems that i cant fiqure out. If anyone can help me please do. thank you so much.

1.

Given an array arr of type int, along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j]. Declare any additional variables as necessary.

something like this??
[CODE] int temp;
temp = *p;
*p = *q;
*q = temp;
[/CODE]


2.Given

an int variable k,
an int array incompletes that has been declared and initialized,
an int variable nIncompletes that contains the number of elements in the array,
an int variable studentID that has been initialized, and
an int variable numberOfIncompletes,
write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes.

You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes.

I have no idea. please help

Comments

  • [b][red]This message was edited by stober at 2003-10-26 15:42:44[/red][/b][hr]
    : Hey i have 2 problems that i cant fiqure out. If anyone can help me please do. thank you so much.
    :
    : 1.
    :
    : Given an array arr of type int, along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j]. Declare any additional variables as necessary.
    :
    : something like this??
    : [CODE] int temp;
    : temp = *p;
    : *p = *q;
    : *q = temp;
    : [/CODE]
    :
    [blue]Above: depends on what p and q are. But most likely thay are not needed. The instructions above gave you 95% of the answer to the problem. all you need in your code in the int variable temp. you can do it with only the folowing variables.[/blue]
    [code]
    int temp;
    int i, j;
    int arr[20];

    [/code]
    :
    : 2.Given
    :
    : an int variable k,
    : an int array incompletes that has been declared and initialized,
    : an int variable nIncompletes that contains the number of elements in the array,
    : an int variable studentID that has been initialized, and
    : an int variable numberOfIncompletes,
    : write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes.
    :
    : You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes.
    :
    : I have no idea. please help
    [blue]read the instructions very carefully. start out by declaring variables that are required in the assignment, and do them one at a time. For example, the first requirement says you need an int variable k. So ...[/blue]
    [code]
    int main()
    {
    int k;

    return 0; // exit program
    }
    [/code]
    [blue]Now just add the remaining variables as instructed. Then initialize the arrays with some dummy data (data that you make up yourself). The final step in the assignment asks you to create a simple loop to do the counting. This should be fairly straight forward if you have read about loops in your text book.[/blue]
    :
    :





  • : Hey i have 2 problems that i cant fiqure out. If anyone can help me
    : please do. thank you so much.
    :
    : 1.
    :
    : Given an array arr of type int, along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j]. Declare any additional variables as necessary.
    :
    : something like this??
    : [CODE] int temp;
    : temp = *p;
    : *p = *q;
    : *q = temp;
    : [/CODE]
    Hmmmm....yeah, you've pretty much got it, if p is a pointer to arr[i] and q is a pointer to arr[j]. But why bother with those? Why not do:-

    [code]int temp;
    temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;[/code]

    : 2.Given
    :
    : an int variable k,
    : an int array incompletes that has been declared and initialized,
    : an int variable nIncompletes that contains the number of elements in
    : the array,
    : an int variable studentID that has been initialized, and
    : an int variable numberOfIncompletes,
    : write code that counts the number of times the value of studentID
    : appears in incompletes and assigns this value to
    : numberOfIncompletes.
    :
    : You may use only k, incompletes, nIncompletes, studentID, and
    : numberOfIncompletes.
    I'm assuming here that the variable numberOfIncompletes has been initialized to a value of 0. If it is not, you're in for a nasty surprise. :-)

    [code]for (k = 0; k < nIncompletes; k++)
    {
    if (incompletes[k] == studentID)
    numberOfIncompletes ++;
    }[/code]

    If this was your homework, can I suggest that you take a look at how the above is working and then re-write it for yourself without looking to make sure you can do it. Otherwise you'll learn jack all. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories