0% found this document useful (0 votes)
7 views3 pages

C/C++ Programming Task Instructions

Uploaded by

shaynayadav2027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

C/C++ Programming Task Instructions

Uploaded by

shaynayadav2027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INSTRUCTIONS

1. Use C/C++ programming language to create the following program:

2. Online Submission on ACCSOFT:

• After completing the task, click clear pictures of output of program.


• Convert the images into a single PDF file.
• Submit the PDF on [Link] .

3. Hard Copy Submission:

• Submit the compiled hard copy file of the task on 11th August 2025, during the first
training session.
• This submission is mandatory and will impact attendance records.
1. Array Input and Output

Q1: Write a program to input 5 integers into an array and display them using a loop.

2. Function to Find Maximum in Array

Q2: Write a function int findMax(int arr[], int n) that returns the maximum element of an
array. Use this function in main().

3. Swap Two Numbers Using Pointers

Q3: Write a function void swap(int *a, int *b) to swap two integers using pointers. Call this
function from main().

4. Structure for Student Data

Q4: Define a structure Student with fields name, roll, and marks. Input and display details for
one student.

5. Array of Structures

Q5: Create an array of 3 Student structures and input/display their data using a loop

6. Pointer to Array

Q6: Write a program to input 5 elements in an array. Use a pointer to access and display the
elements.

7. Sum of Array Elements using Function

Q7: Write a function int sumArray(int arr[], int size) to return the sum of elements of an
integer array.

8. Linear Search Using Function

Q8: Write a function to perform a linear search on an array of integers. The function should
return the index if found, otherwise -1.
9. Reverse Array using Pointers

Q9: Write a program to reverse an array using a pointer (no array indexing allowed for
processing).

10. Factorial Function Assignment Question

Q10: Write a C program to calculate the factorial of a non-negative integer entered by the
user. Implement the factorial calculation using a function. The program should:

• Validate that the input is non-negative.

• Calculate the factorial using either recursion or iteration.

• Display the factorial value.

Bonus:
Modify the program to calculate factorial using a pointer to the integer

Common questions

Powered by AI

Pointers can be utilized to swap two integers by creating a function, void swap(int *a, int *b), where pointers to the integers are passed as arguments. Within this function, a temporary variable stores the value at the first pointer, the first pointer is then assigned the value of the second pointer, and finally the second pointer receives the value stored in the temporary variable. This pointer-based swapping avoids copying values back and forth and directly manipulates memory addresses, providing efficient memory operations .

Using pointers to access elements of an array in C/C++ offers direct manipulation of memory addresses, allowing programmers to iterate over array elements efficiently without using index operators. This is beneficial in scenarios where low-level operations, such as modifying hardware register values or implementing advanced data structures, require pointer arithmetic for performance or design constraints. By incrementing the pointer itself, elements are accessed sequentially, which can lead to more concise and potentially faster-running code as it bypasses additional index calculations and memory bounds checks .

To compute the factorial of a non-negative integer in C/C++, first validate the input to ensure it is a non-negative integer, prompting for re-entry if invalid. The factorial can be calculated iteratively by initializing a result variable to 1 and multiplying it by each integer up to the input number within a loop. Alternatively, a recursive solution could be used, where the function factorial(int n) returns 1 if n is 0 or 1, or n multiplied by factorial(n-1) otherwise. Both solutions achieve the same goal, with recursion providing elegance and iteration offering potential performance benefits depending on compiler optimizations and function call overheads .

The structure Student in C/C++ helps organize student data by grouping related information into a single custom data type. It typically contains fields such as name (a char array or string to store the student's name), roll (an integer for the student's roll number), and marks (a float or integer to store the student's marks). This encapsulation of related data improves code readability and maintains organization when handling multiple student data elements .

Implementing a linear search function, such as int linearSearch(int arr[], int n, int target), enhances a program's ability to locate elements by sequentially checking each element of the array against the target value. If a match is found, the function returns the index of the first matching element. If the target is not found, the function returns -1. This straightforward search method is useful for small or unsorted datasets where binary search is inappropriate .

To reverse an array using pointers in C/C++, two pointers can be employed—one starting at the beginning and the other at the end of the array. By swapping the values that these pointers reference and incrementing the start pointer while decrementing the end pointer, the array can be reversed without using array indexing. This pointer-based method is advantageous in scenarios where memory manipulation and space efficiency are crucial, as it minimizes array overhead and speeds up operations by directly accessing memory locations .

For online submission, after completing the program, capture clear pictures of the program's output and convert these images into a single PDF file. Submit this PDF via the specified Google Forms link (https://forms.gle/SKp5JgiFCJfBcnQQ7). For hard copy submission, compile a physical copy of the completed task and submit it on the specified date, 11th August 2025, during the first training session. This submission is required as it will affect the attendance records. Both steps must be followed accurately to fulfill the submission requirements .

To structure a C/C++ program to accept an array of integers and display them, first declare an array of a fixed size (e.g., int arr[5];). Use a loop, such as for or while, to iterate five times and prompt the user to enter integers, which are stored into the array. Once input is complete, use another loop to iterate through the array and print each integer to the output. This satisfies both input and output requirements using loops .

To calculate the sum of all elements in an integer array using a function, define a function int sumArray(int arr[], int size) that initializes a sum variable to zero. Use a loop, like a for loop, to iterate through each element of the array, adding each value to the sum variable. Finally, return the sum. Practically, in the main function, this function is called by passing the array and its size, allowing for the accumulation and retrieval of the total sum of the array's elements .

The function int findMax(int arr[], int n) is defined to iterate through the given array using a loop, compare each element to find the maximum, and return this maximum value. This can be implemented using a for loop to initialize the maximum with the first element and update it whenever a larger element is found. Within the main function, this function can be called by passing an array and its size as arguments. The returned maximum value could be printed or used for further computations, illustrating value retrieval and usage from an array .

You might also like