How do I write a program that reads and stores a series of integer, first ask for N,then read the value into an array, and calculate the sum of the first N values?
void main() {
int N,i,sum=0,temp;
printf("Please enter value of N\n");
scanf_s("%d", &N);
int *arr = (int*)malloc(sizeof(int)*N);
for (i = 0; i < N; i++) {
printf("Please %d value of array \n",i+1);
scanf_s("%d", &temp);
arr[i] = temp;
sum += temp;
}
printf("Sum Of N Elemetes In Array Is %d", sum);
}
Related
instructions are:
Write a program which reads 10 different integers from the user and finds and prints
a) Minimum element
b) Sum of all elements and
c) Average of elements.
Note: do not use arrays to store the user-entered integers.
i did b and c part like this but i can't a
there is my code:
#include <stdio.h>
void main() {
int n, i, sum = 0;
double avarage = 0;
int min = 0;
i = 1;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
++i;
}
printf("Sum = %d \n", sum);
avarage = sum / 10;
printf("Avg = %.2lf \n", avarage);
printf("Min = %d \n", min);
}
this is output of my code:
How can i print Minimum of those.
you min variable starts with 0, so every number you entered is larger then that.
int min = INT_MAX;
start min with the largest possible integer will guarantee every number you take as input will be smaller
another approach is the use a flag (like boolean) for the first input, and if so directly put it into min:
int min = 0;
i = 1;
int my_flag=0;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
if(my_flag==0){
min=n;
my_flag=1;
}
++i;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, n, m, c[20], s = 0;
int *p[100];
I ask the user to enter the number of polynomial equations to add:
printf("Enter the number of equations to add:\n");
scanf("%d", &m);
and ask the user to enter number of coefficients he is planning to use
printf("Enter the maximum coefficient size of largest equation:\n");
scanf("%d", &n);
for (j = 1; j <= m; j++) {
printf("Enter the coefficients of equation number %d:\n", j);
p[j] = (int*)malloc(n * sizeof(int));
for (i = n; i > 0; i--)
scanf("%d", p[j] + i);
printf("The equation %d becomes as follows:\n", j);
i = n;
printf("%dx^%d", *(p[j] + i), i - 1);
for (i = n - 1; i > 0; i--)
printf("+%dx^%d", *(p[j] + i), i - 1);
printf("\n");
}
the code works ok up to here but I am having problem to add polynomials
printf("On adding all equations we get:\n");
for (i = n; i > 0; i--) {
for (j = 1; j <= m; j++) {
s = s + (*(p[j] + i));
c[i] = s;
}
}
i = n;
printf("%dx^%d", c[i], i - 1);
for (i = n - 1; i > 0; i--)
printf("+%dx^%d", c[i], i - 1);
printf("\n");
return 0;
}
also I dont want to use any kind of other methods if possible... and can we multiply polynomials similarly?
If you change
for(j=1;j<=m;j++){
to
for(s=0,j=1;j<=m;j++){
your code gives correct output if dynamic memory allocated in differenet locations and garbage values are zeros.
You are allocating n*sizeof(int) memory for p[j]th pointer
p[j]=(int*)malloc(n*sizeof(int));
for(i=n;i>0;i--)
scanf("%d",p[j]+i);
That means you can access from p[j]+0 to p[j]+n-1. If read some element to p[j]+n that means you are accessing memory which doesn't belong to you.
I modified code a bit which is working fine
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,m,c[20] = {0},s=0;
int *p[100];
printf("Enter the number of equations to add:\n");
scanf("%d",&m);
printf("Enter the maximum coefficient size of largest equation:\n");
scanf("%d",&n);
for(j=1;j<=m;j++){
printf("Enter the coefficients of equation number %d:\n",j);
p[j]=(int*)malloc(n*sizeof(int));
for(i=n-1;i>=0;i--)
scanf("%d",p[j]+i);
printf("The equation %d becomes as follows:\n",j);
i=n-1;
printf("%dx^%d",*(p[j]+i),i);
for(i=n-2;i>=0;i--)
printf("+%dx^%d",*(p[j]+i),i);
printf("\n");
}
printf("On adding all equations we get:\n");
for(i=n-1;i>=0;i--){
for(s=0,j=1;j<=m;j++){
s=s+(*(p[j]+i));
c[i]=s;
}
}
i=n-1;
printf("%dx^%d",c[i],i);
for(i=n-2;i>=0;i--)
printf("+%dx^%d",c[i],i);
printf("\n");
return 0;
}
Note: Don't forgot to add check for scanf and malloc.
I've written this so far:
#include<stdio.h>
int main()
{
int n = 0, i = 0, sum = 0, a = 0;
scanf("%d", &n);
while (i <= n);
{
scanf("\n%d", &a);
sum = sum + a;
i++;
}
printf("%d", sum);
}
but when I enter 8, for example, it won't allow me to add any other numbers.
What's the problem?
while (i <= n); --> while (i <= n). Drop the ;. With the;, the while() loop never ends and { scanf("\n%d", &a); ... is never entered.
Suggest using auto formatting - easy to catch problems like this.
Also, to read n values use < #BLUEPIXY
// while (i <= n)
while (i < n)
#Shabnam You can use this code
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
My array isn't printing out all the data, just whatever was last inputted. The data should print something like this
For the student number, if not enough numbers are inputted, 0's are automaticaly put in.
/*
Name:
Date: 10/06/2016
Workshop 4
*/
#include <stdio.h>
int main(void)
{
int counter;
int marks [40];
float num_grades = 0;
int row = 1;
float sum = 0;
float average = 0;
int pass = 0;
int fail = 0;
float pass_sum = 0;
float fail_sum = 0;
float pass_average = 0;
float fail_average = 0;
float biggest = 0;
float smallest = 0;
//int grade[40];
int student_num[40];
printf(" ---=== IPC mark Analyser V2.0 ===---\n");
printf("Please enter the number of students(between 3 and 40): ");
scanf("%d", &counter);
while (counter >40 || counter <3)
{
printf("Invalid number, enter a number between 3 and 40 inclusive: ");
scanf("%d", &counter);
}
printf("Row Std No Mrk\n");
printf("--- --------- ---\n");
num_grades = counter;
while (counter > 0)
{
printf("%d ", row);
printf("_____________ ___\r%3d ", row);
scanf("%d", &student_num[40]);
scanf("%d", &marks[40]);
row++;
counter--;
sum += marks[40];
}
for (int i = 0; i < num_grades; i++)
{
printf("%03d %09d %3d\n", row, student_num[40], marks[40]);
}
average = sum / num_grades;
printf("-----------------\n");
printf("-----------------\n");
printf("Marks Entered, printing results:\n");
printf("Row Std No Mrk\n");
printf("--- --------- ---\n");
printf("The average of all marks in this group is %.1f.\n", average);
printf("Program Ended.\n");
return 0;
}
You're always reading/writing index 40 in the student_num and marks arrays, so everything goes to the same place.
Actually, the valid indexes of an array of size 40 are 0 to 39, so you're actually reading/writing off the end of the array, causing undefined behavior.
You need to use the proper index in each place. In the printing loop, use i. In the reading loop, use a variable that starts at 0 goes up to counter.
num_grades = counter;
for (int i = 0; i < num_grades; i++)
{
printf("%d ", i + 1);
printf("_____________ ___\r%3d ", i + 1);
scanf("%d", &student_num[i]);
scanf("%d", &marks[i]);
sum += marks[i];
}
for (int i = 0; i < num_grades; i++)
{
printf("%03d %09d %3d\n", row, student_num[i], marks[i]);
}
scanf("%d", &N);
for( i = 0; i < N; i++ ) {
scanf("%d", &a);
}
printf("%d", a);
If N is bigger than 1, how do I add up the values inputed in each of the loop iterations?
for example.. If N is 2, the scanf will scan twice, and I want the 2 "a" I inputed to add up
int sum = 0;
scanf("%d", &N);
for(i=0;i<N;i++){
scanf("%d", &a);
sum = sum + a;
}
printf("%d", sum);
Now you have a sum variable with 0 value. In your for loop, you go on adding to last value of sum with a and at the end you print sum value.
Create a new variable and add a from the loop so that each inputted value is added up:
int sum=0;
scanf("%d", &N);
for(i=0;i<N;i++){
scanf("%d", &a);
sum+=a; //this is the short for sum=sum+a;
}
printf("The Sum is %d", sum);