It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
#include<stdio.h>
int main(){
int a[50],size,i,big,small;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("Largest element: %d",big);
small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);
return 0;
}
I wrote this code in C language which should find the average, longest character and shortest character from the keyboard...Also this program should terminate when the user input asterisk(*)... I cannot figure it out how should I do the average but I did do something to find longest and shortest character.. Please help???
This does look like homework, but to begin with, there's nothing stopping it from returning 0 after the function does the work, so it'll always terminate.
You'll have to put a check for input, to see if a user types in an asterisk, and then return 0, to stay similar to the way you have it.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
void main()
{
char name[5][10],temp[10];
int i,j;
for(i=0;i<5;i++)
{
printf("\nEnter the name of student:");
scanf("%s",name[i]);
}
for(i=0;i<(5-1);i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n the name of student is:");
for(i=0;i<5;i++)
{
printf("\n%s",name[i]);
}
getch();
}
I couldn't figure out the difference between insertion sort and selection sort ..Is this code following selection algorithm or Insertion ?
It is a specially slow version of selection sort.
It looks like a bubble sort, but a bubble sort would compare/swap the elements at positions j-1 and j, which are contiguous. You compare/swap the elements at positions i and j.
During every iteration of the outer loop, i remains constant while j advances from i+1 to the end. Thus, you end up having the element with the minimum value at position i.
You probably make a lot of unnecessary movements. A proper selection sort would search for the minimum value without moving anything. Then it would swap that minimum value with the value at position i. Therefore, it would perform only one swap per element in the array.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was wondering how you would search through a 2D array (used as a parameter for a function), and find a specific character, e.g. an exclamation mark?
Say I have a 2D array island[20][40] and I want to find the character X. My approach would be to use a nested for loop, to go through each element and an if statement. E.g.
for (i = 0; i < 20; i++) {
for (j = 0; j < 40; j++) {
//Not sure what goes here (I want a function that identifies the element in the array)
if ((some variable) == 88)
printf("The treasure is at: (%d, %d)", i, j);
Thanks for your help :)
-island[20][40] works fine. I just want to know how to search through it for a specific character.
use the condition
if (island[i][j] == 88);
If your array(s) are not ordered then you have no choice but to search through sequentially, there are no short cut.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm writing a program in C, and for error handling it tells you to look for an illegal character when you're scanf-ing. In other words, look for a character that isn't an integer.
It is supposed to display an appropriate error message and terminate the program.
I'm a little confused as to how I go about looking for that illegal character, or noticing that it isn't an integer. Any help?
scanf() returns the number of successful arguments. If you do:
int ivar, return_val;
return_val = scanf("%i", &ivar);
return_val should be 1, cause of 1 parameter (ivar). Check the user input:
if (return_val == 1) {
// right input
} else {
// wrong input
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Basically i want to create a recursive function to implement this program in C
#include <stdio.h>
main()
{
float guess=1,num,num1;
int i;
printf("enter any number:\n");
scanf("%f",&num);
num1=num;
for (i=1;num1>1;i++,num1/=10); //to calculate no of digits in input
i=i/2;
printf("i:%d\n",i); //to make a better guess
for (;i>0;i--,guess*=10);
printf("guess = %f\n",guess);
for (i=1;i<=10;i++) //evaluate square root improving accuracy with each loop
{
guess=(guess+num/guess)/2;
}
printf("sqrt: %f\n",guess);
}
Something like this:
#include <math.h>
#include <float.h>
float MySqrt(float num, float prev)
{
float next = (prev+num/prev)/2;
if (fabs(next-prev)<FLT_EPSILON*next)
return next;
return MySqrt(num, next);
}
To call it, pass 1.0 as your initial guess for the prev parameter.
You can easily make this fail with a stack overflow by passing in bad data, but you probably aren't going to be tested on that in this assignment.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Note: The question has been edited to make the problem go away.
I have written this code to reverse an array using functions. But there is an error in line 24 saying ' ) expected'. I have read it again and again but i couldn't find the error. Can anybody please reveal it and tell me how to remove it?
#include<stdio.h>
#include<conio.h>
#define max 5
/*function prototype*/
void reverse(int[],int);
void main()
{
int arr[max]={1,2,3,4,5};
int i,j;
clrscr();
printf("the list before reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
reverse(arr,max);
printf("\n the list after reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
getch();
}
/*function for reversing elements of array*/
void reverse(int num[],int max)
{
int i,j,temp;
for(i=0,j=max-1;i<max/2;i++,j--)
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
max is defined as a macro. So after preprocessing it becomes
void reverse(int num[],int 5)
Which is not valid and you are getting ' ) expected'. If max is constant then there is no need to pass it as a parameter. And also you have a missing for in the function.
You seem to be missing the for keyword in the loop header in reverse().
EDIT:
Ok, ok my first answer was stupid...
(Answered without thinking enough)
Now I got it:
The problem is
#define max 5
Later max (=5) is used as a parameter!