Question

In: Computer Science

This is C++ programming Given an int variable k, an int array incompletes that has been...

This is C++ programming

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 tried this code down below but it's giving me a logical error:

numberOfIncompletes=0;
for (k = 0; k < nIncompletes; k++)

numberOfIncompletes=0;
for (k = 0; k < nIncompletes; k++)
{

if (incompletes[k] == studentID)
numberOfIncompletes ++;
numberOfIncompletes=studentID;
}

Solutions

Expert Solution

In above code, your mistake is you are incrementing numberOfIncompletes while found studentID in incompletes and you are also assigning studentID to numberOfIncompletes. That's wrong. You only need to increment count.

when you are performing numberOfIncompletes++, it will perform numberOfIncompletes = numberOfIncompletes + 1. Means it directly assigns to numberOfIncompletes. So here your problem is solved.

When you are writing numberOfIncompletes=studentID means every time it will give you studentID in numberOfIncompletes. That is wrong.

You can refer below code for your reference,

Here student ID is 2 and in incompletes it repeats 3 times. Thus, count of numberOfIncompletes becomes 3.

#include <iostream>

using namespace std;

int main()
{
    int k;
    int incompletes[10] = {1, 2, 4, 2, 3, 15, 8, 7, 1, 2};
    int nIncompletes = 10;
    int studentID = 2;
    int numberOfIncompletes = 0;
    
    for (k = 0; k < nIncompletes; k++)
    {
        //when studentID matches,increment numberOfIncompletes by 1 
        if (incompletes[k] == studentID)
        numberOfIncompletes++;
        
    }
    cout<<numberOfIncompletes;

    return 0;
}

Please refer below screenshot of code and output.

In your problem statement it is said that Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. Here assigns the value means whatever count we are getting that assign to numberOfIncompletes. So, that's your mistake.


Related Solutions

Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
A.) myNums is an array of 50 elements of type int and k is an int...
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds? a. for (k = 0; k <= 49; k++)     cout << myNums[k] << " "; b. for (k = 1; k < 50; k++)     cout << myNums[k] << " "; c. for (k = 0; k <= 50; k++)     cout << myNums[k] << " "; d. for (k =...
Given an int variable k, write an expression whose value is the k-th character in the String variable word.
1. Given an int variable k, write an expression whose value is the k-th character in the String variable word. Assume thatword has been initialized and that the value ofk is non-negative and less than the length ofword.Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'i'.Example 2: if k's value is 0 andword is "fall", the value of the expression would be 'f'.2. Given an int variable k, write an expression whose...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string). Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included. 2. Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char**...
Assume that an int variable age has been declared and already given a value and assume...
Assume that an int variable age has been declared and already given a value and assume that a char variable choice has been declared as well. Assume further that the user has just been presented with the following menu: S: hangar steak, red potatoes, asparagus T: whole trout, long rice, brussel sprouts B: cheddar cheeseburger, steak fries, cole slaw (Yes, this menu really IS a menu!) Write some code that reads a single character (S or T or B) into...
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT