Question

6. As a practical joke, a friend gives you an int array for your birthday. As...

6. As a practical joke, a friend gives you an int array for your birthday. As if that weren’t bad enough, your friend tells you that the array contains almost all 0’s except for a small string of consecutive 1’s contained somewhere in the middle. Overwhelmed by the novelty of this you decide to write a function that will print out the location of the first 1 in the array, the location of the last 1 in the array, and the total number of 1’s in the list of consecutive 1 values. Given below is the function prototype where num is the array of int values and max contains the number of elements in the array. void joke (int num[], int max);

7. Write a function day_of_year(month, day, year) that returns the day of the year (an integer between 1 and 360) specified by the three arguments.

8. Write a function num_digits(n) that returns the number of digits in a positive integer n. Hint: To determine the number of digits in a number n, divide n by 10 repeatedly. When n reaches 0, the number of divisions indicates how many digits n originally had.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

6)

int []num = { 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0 };
int firstIndex = Array.IndexOf(num, 1);
int lastIndex = Array.LastIndexOf(num, 1);
int count=0;

for(int i=0;i<num.Length;i++)
{
if(num[i]==1)
{
count=count++;
}
}

Console.WriteLine("First index is " + firstIndex);
Console.WriteLine("Last index is " + lastIndex);
Console.WriteLine("Total count is " + count);
Console.ReadLine();

7)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TasksConsole
{
class Program
{
static void Main(string[] args)
{
string day=day_of_year(3, 17, 1995);
Console.WriteLine(day);
Console.ReadLine();
  
  
}

static string day_of_year(int month, int day, int year)
{
DateTime dt = new DateTime(year,month,day);
return dt.DayOfWeek.ToString();

}
}
}

8)

using System;
using System.Threading.Tasks;
namespace TasksConsole
{
class Program
{
static void Main(string[] args)
{
int val = 63633737;
int count = CountNumberOfDigits(val);
Console.WriteLine(count);
Console.ReadLine();

}
static int CountNumberOfDigits(int number)
{

int numdgits = 0;

do
{

number = number / 10;

numdgits++;

} while (number > 0);

return numdgits;

}


}
}

Solutions are:

Please rate it if the above solution helps you in any way or if you have any concerns comment it, I will help you through again.

Add a comment
Know the answer?
Add Answer to:
6. As a practical joke, a friend gives you an int array for your birthday. As...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Given the prototype: int countPassing(double arr[], int num); Assume that array arr contains num elements with...

    Given the prototype: int countPassing(double arr[], int num); Assume that array arr contains num elements with a value. You may assume that the array has been declared with at least num elements. Write the the function definition including the function header for countPassing() so that the number of values greater than or equal to 69.5 in arr[] is returned.

  • write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • help me solving this both please in c++ language 6 Write function max(int al Lint n)...

    help me solving this both please in c++ language 6 Write function max(int al Lint n) that receives an array of integer and its length, it returns the maximum number in that array. Test your function in main. 7) Write functin reverse (char x[ .int n) that reieves an array of characters and reverse the order of its elements. Test this function in main.

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • Assume you have the following declaration (in main) : int num[10] [7]; Assume that array num...

    Assume you have the following declaration (in main) : int num[10] [7]; Assume that array num is filled completely. Write functions to perform each of the following: a) A function that prints all elements in the array that are greater than 10 or less than 50. b) A function that finds the largest number in the array and returns its subscript. c) A function that finds and returns the average of all the numbers in the array. d) A function...

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT