Question:
A little C programming help please?
Goku Uzumaki
2013-04-15 07:28:12 UTC
I need help in writing 2 programs in C for my class. Before anyone says that "You need to do you own homework", please believe me when I say this - I have been working on this for 3 days now. We had 5 programming questions, I have done 3 but need help with the other two. If anyone could please help me in programming those 2 in C language, I would REALLY appreciate it. Here are the prompts for them:

1) Write a program that uses a 10 element integer array and prompts the user for 10 integer values to populate the array. Create a function that is called from main() that is passed a pointer to the array and returns the largest value stored in the array. The main() function then outputs the largest value contained in the array. [This is what I have for this, don't know if it's right or wrong]

#include
using namespace std;

//function prototypes
int minimum(const int* values, size_t numValues);
int maximum(const int* values, size_t numValues);

int main()
{
const size_t maxValues = 10;
const int SENTINEL = -1;
int i;
int array[i];
int array[maxValues];
int min = array[0];
int max = array[0];
int numValues = 0;


while (array[i] != SENTINEL) //need this to stop if -1 is entered
{
cout <<"Enter up to 10 integers (-1 to stop):" < for (int i = 0; i < maxValues; i++) //loops through array
cin >> array[i];
numValues++;
}

minimum (min, numValues); //not sure how to implement function
maximum (max, numValues); //not sure how to implement function

cout << "Values entered:" << array[i]< cout << "Minimum value:" << min < cout << "Maximum value:" << max <}

//to find max/min
for (i = 0; i < numValues; i++)
{
if (array[i] < min )
{
min = array[i];
}

else if (array[i] > max )
{
max = array[i];
}
}



2) Write a program that prompts the user for a character string. Pass this string to a function called reverse() that you create as part of your program. The function reverse() then reverses the order of all of the characters in the string. The function main() then prints out the reversed string array. [I have NO idea]

Please help!
Four answers:
?
2017-01-21 09:03:45 UTC
1
Jeroonk
2013-04-15 09:11:07 UTC
First things first: is this a C or C++ programming exercise?

In C, there is no such thing as the header "#include ", "cout", "cin", namespaces ("std::") etc. Those are all C++ features. This program should not be able to compile and link using any C compiler.



To handle I/O, instead use "#include " and the functions "printf" and "scanf" from it.



1) Notes on the first program:



-- As said before, don't use cout/cin but printf/scanf instead.



-- On line 13, declaring "int array[i];" is incorrect. You can't declare an array with a variable length (without C99 extensions), and you most definitely can't declare the array with a length which hasn't been specified yet. The variable "i" could have any value.



-- On line 14, you declare a variable-length array (even though "maxValues" has the const qualifier, it is treated as a run-time variable). This is incorrect in C without C99 extensions. Use a #define macro instead.



-- On line 15 and 16, you assign the first value of the "array" to min and max. Because the first element has not been assigned to, it could have any value.



-- On line 20, that while loop references an "array[i]", which is undefined, because "i" has no value. You probably meant to move this inside the for-loop instead, as an if (see the attached code below). The way you have written it (using 2 nested loops), the user is asked continuously to enter 10 values, and the program will never exit the while-loop.



-- On line 23, you re-declare "int i" inside the loop initialization. This is not allowed in C (without C99 extensions). Instead, use the variable you already declared at the top of "main".



-- The functions minimum and maximum return an int. Call them like this:

min = minimum(array, numValues);

max = maximum(array, numValues);



-- You can not pass a whole array to the output at once. Also note that "array[i]" does not mean 'the array'. It means 'the i-th element of array'. Since you just exited a for-loop, the value of "i" will be 10, but the array does not have an 11th element (N.B. indices start at 0).



-- "int main()" is a function that returns an int, as operating system is expecting a status number. Without a return statement, this code should not compile. Add the following line at the end to indicate a successful completion of the program:

return 0;



-- See the attached code below for an example on how to write function definitions of "minimum" and "maximum".



Corrected code: http://pastebin.com/fhB4BCQz



2) For this program, there are two main problems:



-- Reading in a string of user input. This can be done using "scanf("%s", str);", where "str" is a char-array. The problem is that the user can enter too long a string, and that the string will be cut off at the first whitespace entered.



Another way is to use "gets(str);", which reads one line of user input. This suffers the same problem that it might read a string too long to fit in your array.



The correct way to do this is using "fgets(str, MAX_LENGTH, stdin);", which will read in one line of user input, but only up to MAX_LENGTH characters, which is the length of our char-array "str".



Be aware that this function does leave the trailing new-line "\n" at the end of the string. We can remove that by using the function "strchr" from , which gives us a pointer to the first occurrence of a character in the string.



-- Reversing the string. This can be done by swapping the first and last character, the second and second-to-last character etc.



To iterate in this way, we first need to find the length of the string using "strlen" from . Then we swap all characters "str[i]" with "str[n-i-1]", with n being the length of the string, iterating "i" from 0 to n/2.



Inspect the attached code for reference: http://pastebin.com/EWEtz1fw
Fred W
2013-04-15 08:51:00 UTC
int minimum(const int* values, size_t numValues);

{

if (numValues == 0) {

fprintf(stderr, "need at least 1 element!\n");

exit(1);

}

int m = values[0];

for (size_t i = 1; i < numValues; ++i)

if (values[i] < m) m = values[i];

return m;

}



int maximum(const int* values, size_t numValues);

{

if (numValues == 0) {

fprintf(stderr, "need at least 1 element!\n");

exit(1);

}

int m = values[0];

for (size_t i = 1; i < numValues; ++i)

if (values[i] > m) m = values[i];

return m;

}



/* we use two functions to ensure that parameter is writable */

#include

#include



/* The reverse of the empty string is empty

The reverse of a one character string is one character

Otherwise, the reverse is the last character append the reverse of the first characters

*/

char *reverse2(char *s)

{

char *t;

if (strlen(s) <= 1) return s;

t = strdup(s);

t[0] = s[strlen(s) - 1];

t[1] = 0;

s[strlen(s) - 1] = 0;

strcat(t, reverse2(s));

strcpy(s, t);

free(t);

return s;

}



/* make sure the parameter is writable for reverse2() */



char *reverse(char *s)

{

char *t = strdup(s);

return reverse2(t);

}



/* test reverse string function */

int main()

{

char *s;

printf("%s\n", s = reverse(""));

free(s);

printf("%s\n", s = reverse("a"));

free(s);

printf("%s\n", s = reverse("ab"));

free(s);

printf("%s\n", s = reverse("abc"));

free(s);

printf("%s\n", s = reverse("abcd"));

free(s);

return 0;

}
?
2016-11-03 09:20:26 UTC
int considerable(int agrc, char *argv[]) { /* argv[0] is application call agrv[a million] is first command line argument it relatively is a string. like this argv[i] is a ith string i did no longer comprehend what's your question is. permit you prefer to traverse this ith string charater by ability of character. you are able to hire 2-D character array form as shown decrease than*/ int i=a million, j; /* i'm assuming you prefer to traverse 1st command line argument character by ability of characetr*/ for(j=0; argv[i][j]!='0'; j++) printf("%c", argv[i][j]); return 0; } SORRY i did no longer comprehend YOUR question in any respect }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...