I was having a trouble regarding to printing out the largest number in array in c language and i don't know how to do it while using loop. Please help me thanks
here is my code
#include <stdio.h>
void main() {
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for (int i = 0; i < size; i++) {
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for (int i = size - 1; i >= 0; i--) {
printf("%d ", nums[i]);
}
for (int a = 0; a < size; a++) {
for (int i = 0; i < size; i++) {
if (nums[a] > nums[i]) {
Lnum = nums[a];
}
}
}
printf("\nLargest element = %d", Lnum);
}
The algorithm for determining the largest element in an array goes as follows.
Use the value of the first array element as a start value for the biggest number.
Go over the rest of the array and check for each element if it is a bigger number.
When a bigger number is found use the bigger number for further comparisons.
#include<stdio.h>
void main(){
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for(int i=0; i<size; i++){
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for(int i=size-1; i>=0; i--){
printf("%d ", nums[i]);
}
Lnum = nums[0]; // Assume first element to be the largest element
for(int a=1; a<size; a++){ // start loop with second element
if(Lnum < nums[a]) { // check if current element is larger
Lnum = nums[a]; // found larger element, it is now the largest element of all inspected element
}
}
printf("\nLargest element = %d", Lnum);
}
Output:
Enter the size: 3
1 2 3
Reversed = 3 2 1
Largest element = 3
...Program finished with exit code 0
Related
So I need to write a program which takes (by user input) a integer array of 100 elements and finds the max of every 5 elements and writes them in a new array and adds the rest of the numbers to another array.
So an example would be:
original array (array1): 1,23,6,7,16,19,24,56,99,43 ...
soo it will take the first 5 elements: 1,23,6,7,16 and find the max value (23) and add it to the new array
then the next 5 : 19,24,56,99,43 find their max (99) and add it to the other array and so on until it reaches the last number.
second array (array2): 23,99
third array (array3): 1,6,7,16,19,24,56,43
I've tried everything, but i can only get either the Max number in the WHOLE array or the max number of the FIRST 5 elements.
This is how my program should look like:
#include <stdio.h>
#include <stdlib.h>
int array1[100];
int array2[100];
int size;
int main() {
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size > 100) {
return 0;
}
printf("\nEnter %d numbers: \n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
}
printf("\nEntered array: ");
for (int i = 0; i < size; i++) {
printf("%d, ", array1[i]);
}
printf("\n");
// the code for the max numbers should be here
printf("The max values of every 5 elements are: \n");
for(int i = 0; i < size2; i++) {
printf("%d, ", array2[i]);
}
}
"size2" is the size of the new array.
#include <stdio.h>
#include <stdlib.h>
int array1[100];
int array2[100];
int array3[100];
int size, size2, size3;
int main() {
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size > 100) {
return 0;
}
printf("\nEnter %d numbers: \n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
}
printf("\nEntered array: ");
for (int i = 0; i < size; i++) {
printf("%d", array1[i]);
if(i%5==4) printf("| ");
else printf(", ");
}
printf("\n");
// the code for the max numbers should be here
size2=0; size3=0;
int maxvalue_index=0;
for(int i = 1; i < size; i++) {
if(array1[i]>array1[maxvalue_index]) maxvalue_index=i;
if(i%5==4 || i==size-1){
array2[size2]=array1[maxvalue_index];
size2++;
for(int j=i-(i%5); j<=i ; j++){
if(array1[j]==array1[maxvalue_index]) continue;
array3[size3]=array1[j];
size3++;
}
maxvalue_index=i+1;
}
}
printf("The max values of every 5 elements are: \n");
for(int i=0;i<size2;i++) printf("%d ",array2[i]);
printf("\nArray3\n");
for(int i=0;i<size3;i++) printf("%d ",array3[i]);
printf("\n");
}
Try this, It will work for non 5*n lengths
Let me know if there is any error :)
The function below finds the maximum of elements in the array from start to end exclusive.
int findmax(int *const arr, const size_t len, const size_t begin, size_t end) {
int res = arr[begin];
end = (len < end) ? len : end;
for (int i = begin+1; i < end; i++) {
if (arr[i] > res)
res = arr[i];
}
return res;
}
Define this function before your main, then call it for every five elements; the maximum of the first five elements is findmax(array1, size1, 0, 5), for the next
five it would be findmax(array1, size1, 5, 10) and so on and so forth. It shouldn't take that much effort to make a for loop that automates the process.
I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}
I am new to C and I have a problem. I need to reverse the elements of arrays in the following program. Can I get a very simple explanation of what I am doing wrong and how I could go on about fixing it?
Here's the output I get:
Enter number 0: 0
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Element 0 is: 7
Element 1 is: 6
Element 2 is: 5
Element 3 is: 4
Element 4 is: 4
Element 5 is: 5
Element 6 is: 6
Element 7 is: 7
My code:
#include <stdio.h>
void reverse(int a[], int i)
{
int j=7,b;
for (i=0; i<=7; i++)
{
b=a[i];
a[i]=a[j];
a[j]=b;
printf("Element %d is: %d\n", i,a[i]);
j--;
}
}
int main(void)
{
int a[8];
int i;
for(i=0;i<=7;i++)
{
printf("Enter number %d: ",i);
scanf("%d", &a[i]);
}
reverse(a, 8);
return 0;
}
To avoid being misled, write the output after the reversal, not during it:
#include <stdio.h>
void reverse(int a[], int i)
{
int j = 7, b;
for (i = 0; i <= 7; i++)
{
b = a[i];
a[i] = a[j];
a[j] = b;
j--;
}
}
int main(void)
{
int a[8];
int i;
for (i = 0; i <= 7; i++)
{
printf("Enter number %d: ",i);
scanf("%d", &a[i]);
}
reverse(a, 8);
for (i = 0; i <= 7; i++)
{
printf("Element %d is: %d\n", i, a[i]);
}
return 0;
}
Now you’ll notice the array isn’t changing at all. That’s because you’re swapping every element in the array twice: once to its reversed position, then back to its original position. To fix this, only loop over the first half the array, i.e. while i < 4.
It was probably also intended to make the second argument to reverse the length, so you should make use of that instead of hard-coding 7 or 4:
void reverse(int a[], int length)
{
int i, j = length - 1, b;
for (i = 0; i < length / 2; i++)
{
b = a[i];
a[i] = a[j];
a[j] = b;
j--;
}
}
You could also copy over the array in a new one:
#include <stdio.h>
void reverse(int a[], int i)
{
int b[i];
int j;
for(j=0; j<i; j++){
b[j] = a[i-1-j];
printf("Element %d is: %d\n", j, b[j]);
}
}
int main(void)
{
int a[8];
int i;
for(i=0;i<=7;i++)
{
printf("Enter number %d: ",i);
scanf("%d", &a[i]);
}
reverse(a, 8);
return 0;
}
What you want to do it move through only half of the length of your array else you're reversing the array twice.
void reverse(int a[], int len)
{
int i;
int j=len-1,b;
for(i=0;i<len/2;i++)
{
b=a[i];
a[i]=a[j];
a[j]=b;
j--;
}
for(i = 0; i < len; i++) {
printf("Element %d is: %d\n", i,a[i]);
}
}
The Principe is simple. just stand at the middle of the table and reverse the elements as follow. so pose n is lent of the table. then the last element is n-1.
so you ave to reverse position t[0] and t[n-2]
reverse t[1] and t[n-3] and so on...
stop when you rich the middle of the table. try to code your self is better than for me giving you the code.
hope it helped.
I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9
School project for Computer Science. I need to make a program where the user declares a size for an array, then fills the array in numerical, nondecreasing order, then declares a value, x. X is then assigned to the appropriate spot so the entire array is in numerical, nondecreasing order. The array is then output.
The code builds properly with no errors, but the output is messed up.
#include <stdio.h>
int main (void) {
//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;
//Statements
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) {
scanf("%d", &ary[i]);
}
size++;
printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);
while(i=0 <= x && x > ary[i]){
i++;
j = size - 1;
while(j >= i) {
ary[j++] = ary[j];
j--;
}
}
for(i = 0; i < size; i++) {
printf("%d,", ary[i]);
}
return 0;
} //main
Example Output:
Enter the size of the array: 5
Enter digits to fill the array, in numerical order: 1
2
3
4
5
Input x, the value to add to the array: 6
1,2,3,4,5,1630076519,
Process returned 0 (0x0) execution time : 8.585 s
Press any key to continue.
It's always the last value that messes up to that huge number. I cannot for the life of me figure out why. The project is due by midnight EST.
For the while loop, can you try this instead,
i = 0;
while (i < x && x > ary[i]) {
i++;
j = size - 1;
while (j >= i) {
j++;
ary[j] = ary[j]; // Equivalent to ary[j++] = ary[j];, yet easier to read
j--;
}
}
Try this:
#include <stdio.h>
int main (void) {
//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;
int temp1,temp2;
//Statements
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) {
scanf("%d", &ary[i]);
}
printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);
for(i=0;i<size;i++)
{
if(ary[i]>x)
{
temp1 = ary[i];
ary[i] = x;
break;
}
}
if(i==size)
{
ary[i]= x;
}
else
{
for(j=i+1;j<size+1;j++)
{
if(j==size) //Last element of the new array
{
ary[j] = temp1;
break;
}
temp2 = ary[j];
ary[j] =temp1;
temp1 = temp2;
}
}
for(i = 0; i < size+1; i++) {
printf("%d,", ary[i]);
}
return 0;
} //main