exercise about vector and function - c

Thanks a lot, with you help I understood all my mistakes (:
This is my first time using this website so I'm not sure if it is in the right format.
Basically I have to make a function that fills a vector, but it isn't working at all.
English isn't my first language so this is probably really confusing, but I'd appreciate if
somebody helped me. Thanks a lot.
#include <stdio.h>
#include <stdlib.h>
void le_vet(int v[1000], int n, int i)
{
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
int main()
{
int v[1000], n;
printf("Type the syze of the vector: ");
scanf("%d", &n);
void le_vet(n);
system ("pause");
return 0;
}

You are not calling le_vet in your main function, you are rather doing something more along the lines of creating a function pointer called "le_vet" that takes an int (by default, as no type is specified) and returns a void. I'm pretty sure this is not what's intended.
Instead, change void le_vet(n) to le_vet(v, n) and change this:
void le_vet(int v[1000], int n, int i)
{
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
to this:
void le_vet(int v[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
Since you're not needing to pass i in from outside the function, there's no need to include it in the arguments to the function. The first element in a for loop is executed once right as the loop is entered, therefore it is often used to declare the iteration variable for the loop, as I did here.
EDIT: Whoops. Can't do that in C. I'm to used to C++ that I made a goof here. Declare i just above the loop, as #Over Flowz suggests. Updating my revised code, but leaving this record as evidence that it's time to stop working and go eat dinner :)

You are only passing one argument to le_vet(), when it requires three arguments. You also need to remove the void, since you are calling on a function.
Maybe this will work.
void le_vet(int n)
{
static int v[1000];
for (int i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
You don't need the int i passed as a parameter, since you are creating another one in the for loop.
int i = 0;
while (i < n)
{
i++;
}
is the same as
for (int i = 0; i < n; i++)

When you invoke like this:
...
scanf("%d", &n);
void le_vet(n); //you are declaring a function. You need to remove the void keyword
system ("pause");
...
You should invoke like this:
...
scanf("%d", &n);
le_vet(n);
system ("pause");
...
Then you will see the real errors, like the number of parameters

try:
#include <stdio.h>
#include <stdlib.h>
void le_vet(char v[], int n)
{
int i = 0;
for(i = 0; i < n; i++)
{
printf("Type the number %d: ", i+1);
scanf("%s", &v[i]); //Read string, not decimal for output.
}
}
int main()
{
char v[1000] = {0}, n;
printf("Type size of the vector: ");
scanf("%d", &n);
le_vet(v, n);
printf("%s", v);
system("pause");
return 0;
}
Hope it helps.

Related

I want to make a program in C which takes an array as an input and tells which number are perfect squares

#include<stdio.h>
#include<math.h>
int perfectSquare(int arr[], int n);
int main()
{
int n , arr[n];
printf("number of elements to store in array");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("enter %d number", i+1);
scanf("%d", &arr[i]);
}
perfectSquare(arr, n);
return 0;
}
int perfectSquare(int arr[], int n)
{
int i;
int a;
for (i = 0; i <= n; i++) //i=4 arr[4]==9 //arr[1]=2 i=1
{
a=sqrt((double)arr[i]); //a=3 //a=1.454=1
if ( a*a==arr[i] ) //a==3*3==9==arr[4] //a*a=1!=arr[2]
printf("%d", arr[i]);
}
}
I am new to coding and I am currently learning c. I came up with this code but it doesn't work can someone tell me what is the problem with this code?
There are a couple of issues with this exercise, but generally you're on the right track. Here, a version of your example with some possible corrections:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void perfect_square(int arr[], int n);
int main(void)
{
int i, n, *arr;
printf("number of elements to store in array: ");
scanf("%d", &n);
if (n <= 0)
return -1;
arr = malloc(n*sizeof(int));
if (arr == NULL)
return -2;
for (i = 0; i < n; i++) {
printf("enter number %d: ", i+1);
scanf("%d", &arr[i]);
}
perfect_square(arr, n);
free(arr);
arr = NULL;
return 0;
}
void perfect_square(int arr[], int n)
{
int i, a;
for (i = 0; i < n; i++) {
a = (int)sqrt((double)arr[i]);
if (a*a == arr[i])
printf("%d ", arr[i]);
}
}
Some hints:
Arrays, that have an unknown size at compile time are usually allocated with malloc(), and must be deallocated again with free() (see also: alloca(), calloc(), realloc()). (In "more recent versions of C" there is also the possibility to use variable length arrays, but those can limit the portability of the code).
Always make sure to check the start value and end condition of for-loops, to prevent out of bound errors.
And try to consistently format the code, use good names and nice indentation to improve read-, maintain-, reusablilty.

How to repeat scanf() n times?

I ask the user to input two integers, n and x. After that, I need to ask them for a value for the a variable n times. I need to create a new a variable for each value. How do I do it? I have absolutely no idea.
Also, I need to do it in one line, so the input will be, for example, 50 30 21, not
50
30
21
Thanks.
#include <stdio.h>
int main (void) {
int a, n, x;
int i = 0;
scanf ("%d%d", &n, &x);
scanf ("%d", &a); /* what should I do here? */
}
Try this:
int arr[100]; // static allocation but you can also allocate the dynamically memory
printf("Enter the number for how many time repeat scanf()\n");
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
Try the below code:
#include <stdio.h>
int main (void) {
int a[100];
int n, x;
int i = 0;
scanf ("%d%d", &n, &x);//n cannot be greater than 100 in this case.
for(i = 0; i < n; i++)
{
scanf ("%d", &a[i]);
}
return 0;
}
Using dynamic memory:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int *a = NULL;
int n, x, i;
scanf("%d%d", &n, &x);
if (n <= 0) {
fprintf(stderr, "n must be > 0\n");
return 1;
}
a = malloc(n * sizeof(int));
if (a == NULL) {
fprintf(stderr, "failed to allocate memory for "
"%d integers\n", n);
return 1;
}
/* reading the user input */
for (i = 0; i < n; i++) {
scanf ("%d", &a[i]);
}
/* usage */
for (i = 0; i < n; i++) {
printf("a[%d] = %d\n", i, a[i]);
}
printf("x = %d\n", x);
free(a);
return 0;
}
After that, I need to ask them for a value for the a variable n times.
High Level Programming Languages and even Assembly Language (Conditional/Unconditional Jump) provide Loop constructs to a programmer. In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached. In C/C++, we have for, while, do while loop constructs.
So, i order to ask a user for a value for n times, you can use for loop in your program. I have given an example below:
#include <stdio.h>
#define MAX_ARR_LEN 100
int main (void)
{
int a[MAX_ARR_LEN];
int n, x;
int i = 0;
scanf ("%d%d", &n, &x);
if (n > MAX_ARR_LEN) {
printf("You can't enter more than - %d\n", MAX_ARR_LEN);
return -1;
}
for(i = 0; i < n; i++) {
scanf ("%d", &a[i]);
}
return 0;
}
When you do scanf, it takes the input until the space character, which is a delimiter. You do not need to specify anything extra for that.
If you do not need to use the value of n again, you could use this code.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int n, x, i;
scanf ("%d%d", &n, &x);
int *a = (int *) malloc (n * sizeof(int)); //This will allocate 'n' integer sized memory for 'a'
for(i = 0; i < n; i++) {
scanf ("%d", &a[i]);
}
}

C Program, User input into a single array

I'm trying to use three integers that are inputted by a user in a single line into an array using CodeBlocks. But i don't really know how to go about this. Can anyone help me?
Thanks!
main()
{
int arr[3];
int onenum;
int twonum;
int threenum;
printf("Enter an Input: ");
scanf("%d %d %d",&onenum,&twonum,&threenum);
printf("Your input is: %d %d %d \n",onenum,twonum,threenum);
int arr [onenum, twonum, threenum];
return 0;
}
Use this
int i;
int arr[3];
printf("Enter numbers");
for(i=0;i<3;i++){
scanf("%d",&arr[i]);
}
This will store the 3 numbers entered by user in array arr.
Like this:
void main()
{
int arr[3];
int i;
printf("Enter an Input: ");
for(i = 0; i < 3; i++)
{
scanf("%d", &arr[i]);
}
printf("Your input is: ");
for(i = 0; i < 3; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Let's use a loop from which runs from i=0 to i=2 because you have to add 3 numbers in array.
void main()
{
int arry[3],i=0;
printf("enter numbers");
for(i=0;i<3;i++)
{
scanf("%d",&arry[i]);
}
for(i=0;i<3;i++)
{
printf("%d \n",arry[i]);
}
}
Array is a collection of data.
For beginning you can think a array with different index as a different variable of same data type means arry[0] and arry[1] in this example as different variables of same data type integer.It will help you for beginning with array but keep in mind that arry is the only one variable the index inside capital bracket tells the variable where to look.

How do I save a variable read in a function in C?

Let's say I have this function:
void read() {
int num;
scanf("%d", &num);
}
and I want to use the num variable in another function like this:
void func() {
for (i = 0; i < num; i++)
printf("%d ", i);
}
But when trying to compile it says the variable is uninitialized. I know that it most likely requires pointers, but I can't get a hang of it.
There are two ways to go about doing this:
Return the variable that you read to a caller, or
Have the caller provide space for your variable, and store it there.
The first approach requires you to change the return type to int:
int read() {
int num;
scanf("%d", &num);
return num;
}
void func() {
int num = read();
for (i = 0; i < num; i++)
printf("%d ", i);
}
The second approach requires you to take an int* pointer:
void read(int* p) {
scanf("%d", p);
}
void func() {
int num;
read(&num);
for (i = 0; i < num; i++)
printf("%d ", i);
}
You func should be,
void func(int num) {
for (i = 0; i < num; i++)
printf("%d ", i);
}
And you also need to pass value like,
void read() {
int num;
scanf("%d", &num);
func(num);
}
You don't always need pointers - you can simply return the value and then use it.
int read() {
int num;
scanf("%d", &num);
return num;
}
You can now store the value returned from the function for future sue, e.g.
void func() {
int num = read();
for (i = 0; i < num; i++)
printf("%d ", i);
}
You can make the variable global and inside the function:
void func() {
int i;
for(i=0;i<num;i++)
printf("%d ", i);
}

Sorting an array in C

I'm trying to sort elements in an array from smallest to largest that a user inputs along with the size. This is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXVALUES 20
void sort(int *A[], int n) {
int i, tmp, j;
for (i = 0; i <= (n - 2); i++) {
for (j = (i + 1); j <= (n - 1); j++) {
if (*A[i] > *A[j]) {
tmp = *A[i];
*A[i] = *A[j];
*A[j] = tmp;
}
}
}
return;
}
int main(int argc, char *argv[]) {
int n, A[MAXVALUES], i;
printf("Enter an array and no. of elements in array: ");
scanf("%d%d", &A[MAXVALUES], &n);
sort(A[MAXVALUES], n);
printf("Ordered array is: \n");
for (i = 0; i <= (n - 1); i++) {
printf(" %d", A[i]);
}
return 0;
}
The compiler compiles it without any errors but it stops working after I put in the inputs. I've yet to quite grasp the theory behind arrays and pointers so could someone tell me where in my code I'm going wrong?
scanf("%d%d", &A[MAXVALUES], &n); is the problem
That's not how you read an array.
First you read the n, after that inside a loop you read every element like
scanf("%d", &A[i]); where i is the index from 0 to n
EDIT:
scanf("%d", &n);
int i = 0;
for(i = 0; i < n; i++)
{
scanf("%d", &A[i]);
}
This is what you want.
You can't use scanf() to read in a whole array at once.
This:
scanf("%d%d", &A[MAXVALUES], &n);
Makes no sense; it passes scanf() the address of the element after the last one in A, causing undefined behavior. The sort() call is equally broken.
To read in multiple numbers, use a loop. Also, of course you must read the desired length first, before reading in the numbers themselves.
Firstly I'll tell you a couple of things about your sorting function. What you want, is take an array (which in C is representable by a pointer to the type of elements that are in that array, in your case int) and the array's size(int) (optionally you can take a sorting method as an argument as well, but for simplicity's sake let's consider you want to sort things from lowest to highest). What your function takes is a pointer to an array of integers and an integer (the second argument is very much correct, or in other words it's what we wanted), but the first is a little more tricky. While the sorting function can be wrote to function properly with this argument list, it is awkward and unnecessary. You should only pass either int A[], either int *A. So your function header would be:
void sort1(int *A, int n);
void sort2(int A[], int n);
If you do this however, you have to give up some dereferencing in the function body. In particular I am referring to
if (*A[i] > *A[j]) {
tmp = *A[i];
*A[i] = *A[j];
*A[j] = tmp;
} which should become
if (A[i] > A[j]) {
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
You should check out the operators [] and *(dereference) precedence http://en.cppreference.com/w/c/language/operator_precedence
Now, to adapt to these changes (and further correct some code) your main would look like so:
int main(int argc, char *argv[])
{
int A[MAXVALUES], n;
do{
printf("n="); scanf("%d", &n);
}while(n < 0 || n > 20);
int i;
for(i = 0; i < n; ++i)
scanf("%d", &A[i]); //this will read your array from standard input
sort(A, n); //proper sort call
for(i = 0; i < n; ++i)
printf(" %d", A[i]);
printf("\n"); //add a newline to the output
return 0;
}

Resources