Question

Part 1: Determine what is wrong (if any) with the following statements int A[4] = {...

Part 1:

Determine what is wrong (if any) with the following statements

  1. int A[4] = { 1, 2, 3, 4, 5};
  2. int A[4] = {1,2};
  3. int A[4] = [1,2,3,4];

Part 2:

Create one dimensional array and save the following integers in it:  2, 3, 6, 7, 10, 12. Display the values on the screen. (for example, a[0] = 2, a[1] = 3, etc.)

Part 3:

Write a program to read 5 integers from the user. Save these integers in an array and display the following on screen:

  1. The entered values
  2. The average value

Part 4:

Create a function that will do the following (when called):

Generate 5 random numbers between 1 – 10. (Hint: use rand() function)

Save the random numbers in an array (Hint: create an array)

Display these numbers on the screen by reading them from the saved array.

Homework Answers

Answer #1

Part 1:

  1. int A[4]={1,2,3,4,5}; -> This will give us error because the array is declared with size 4 but five elements are given. So the array cannot initialize the number 5.
  2. int A[4] = {1,2}; -> This will not cause any error. Though the size is declared 4, the remaninig elements will be 0. The array will be {1,2,0,0}
  3. int A[4] = [1,2,3,4]; -> The initialization is wrong here. This is not the correct syntax for initialization. Use curly braces {} instead of brackets.

Part 2:

#include <stdio.h>

int main()
{
   int a[6]={2,3,6,7,10,12};  //single dimension array declared and initialized with the given values
    printf("The values of array a[] are: ");
    for(int i=0;i<6;i++)
    {
       printf("%d ",a[i]);//printing the array values
    }

    return 0;
}

Output: The values of array a[] are: 2 3 6 7 10 12

Part 3:

#include <stdio.h>

int main()
{
    int a[5],i,sum=0;  //single dimension array declared with size 5
    double average;
    printf("Input the 5 values one by one : \n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&a[i]); //Input the array values
    }
    printf("Entered 5 values are : \n");
    for(i=0;i<5;i++)
    {
        printf("%d ",a[i]); //Input the array values
    }
    printf("\n"); //new line
    for(i=0;i<5;i++)
    {
        sum=sum+a[i]; //add all the values in array to sum
    }
    average=sum/5; //calculate average
    printf("The average of all the 5 values is: %.2f ",average);
    return 0;
}

Output: The ouput for the above code is,

Input the 5 values one by one :
1
2
3
4
5
Entered 5 values are :
1 2 3 4 5
The average of all the 5 values is: 3.00

Part 4:

#include <stdio.h>
#include<time.h>
#include<stdlib.h>
void generate()
{
    int a[5],i;
    printf("Generating 5 random values between 1 and 10\n");
    for(i=0;i<5;i++)
    {
        a[i]=rand()%10+1; //here we generate random values between 1 and 10 using rand() function and store them in array
    } 
    printf("Values generated and stored in array\n");
    printf("Displaying the random generated values: ");
    for(i=0;i<5;i++)
    {
        printf("%d ",a[i]); //display the random values
    }
}
int main()
{   srand(time(0));  //this will give the rand() a new seed so that you will get newly generated random values every time. If you dont use this you will get the same values every time you execute the program.
    generate(); //call the generate() function
    return 0;
}

Output:

Generating 5 random values between 1 and 10
Values generated and stored in array
Displaying the random generated values: 4 7 8 6 4

#Please dont forget to upvote if you find the solution helpful. Thank you.

Know the answer?
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
Similar Questions
Class work # 12 / Chapter 6 –> Arrays 3 (two dimensional arrays) Part 1: Create...
Class work # 12 / Chapter 6 –> Arrays 3 (two dimensional arrays) Part 1: Create a 2-by-3 integer array and initialize it with 6 integers of your choice. Using nested for loops, display the contents of the array on the screen Part 2: (Continue on part 1) Display the sum of all the integers in the array that you created in part 1. Create a function Part 3: Create a function that will display the elements of the array...
1- Create a new script that is called HW9.m and save it. The script will do...
1- Create a new script that is called HW9.m and save it. The script will do the following. Test your code for each of the cases and upload your script as a .m file. [15 points] a. Create a random integer numbers that goes from 0 to 5 and assign it to a variable y using rand and round commands. Hint: Note that rand function only creates random number between 0 and 1. You need to scale the values to...
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
Complete following function which receives an array of integers and the length of the array, and...
Complete following function which receives an array of integers and the length of the array, and then returns the sum of all the positive numbers in the array. For example, if an array that is passed to this function contains following numbers: -1, 2, 0, 3, 4, -3, 0, 2, 0, and then the return value of the function should be 11. Will this function be working correctly? Yes or No? int sumPositive(int a[],int length) { int s=0;     for(int...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
1. What are the inputs to Excel’s RAND function? a.There is one argument to put into...
1. What are the inputs to Excel’s RAND function? a.There is one argument to put into the function – the distribution from which to generate random numbers. b.There are two arguments to put into the function – the number of random values to generate, followed by the number of decimal places. c.The RAND function has no arguments, just parentheses with nothing in between. 2. What do you actually type into a spreadsheet cell to use the RAND function? a. =RAND()...
write a program that automates the process of generating the final student report for DC faculty...
write a program that automates the process of generating the final student report for DC faculty considering the following restrictions. Consider declaring three arrays for processing the student data: studID studName studGrade The student ID is a random number generated by the 5-digit system in the range of (10000 - 99999). Create a function to assign the student ID generated in an array of numbers. Consider the following to generate the random number: Add the libraries: #include <stdlib.h> #include <ctime>...
Create a 1D numpy array that contains int values (4, 3, 1, 33, 21, 67, 27,...
Create a 1D numpy array that contains int values (4, 3, 1, 33, 21, 67, 27, 89, 34, 67, 99, 89, 12, 43). Hint: You can put these values in a python list and start from there. Write code that prints values (67, 27, 89, 34). You must use the colon : based slicing approach to complete this task. Your code should print the following: [67 27 89 34] Write code to print the average (mean) and median values for...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Given this array: int sequence[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 1,...
Given this array: int sequence[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 }; Write a C++ program to ask the user to enter a number, if the number can be found in the array, your program should display "Found". Otherwise, your program should display "Not found". For example, if the user enters 7, your program should display "Found". If the user enters 11, your program should display "Not found".